Sign in to follow this  

(Matlab) Multiply whole array?

Recommended Posts

I never get any answer at the matlabcentral/answers so i try here!
I am modifying all values in a column and the below code works:
for i=1:size(A)   density=A(i,pressPos)*100/(287.05*(A(i,tempPos)));   A(i,windPos)=A(i,windPos)*power(density/densityNormal,1/3);end

But this is very slow as i have 200 000 rows in the matrix. Any way to do it column-wise instead of once per row? I try this but then i get memory problem and 'error using \' (see errors under the code)


Error using \
Out of memory. Type HELP MEMORY for your options.
Error in scaleWind (line 31)
Error in getLongData (line 19)
A = scaleWind(A,avgWind); Edited by suliman

Share this post

Link to post
Share on other sites

It's difficult to help "optimize" code when you don't know what the code is doing and what the dimensions of the variables are. Make a complete example with actual values, but with manageable sizes, such as just 10 rows instead of 200000.

Share this post

Link to post
Share on other sites

I second Brother Bob's comment. However, from visual inspection, try using element-wise division with the ./ operator. That might help because it seems in the second code example "density" is a vector and you're trying to divide a vector by a vector. Indexing matrices takes a while with large matrices and the more you can do with matrix-type operations, the faster the code will run.


Also, Matlab was never built for speed. It can run fast, but it's main forte that it tries to use the best algorithms based on your inputs. If you're doing Ax=B and your matrix is symmetric positive definite, you don't have to specifically choose Cholesky over LU decomposition. It should figure that out. If you're looking for speed, try using LAPACK or other such libraries. Your code may still be slower than Matlab's, but at least then you can better optimize it to suit your specific problem.


Hope that helps.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this