(Matlab) Multiply whole array?

Started by
2 comments, last by suliman 11 years ago
Hi
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)
density=A(:,pressPos)*100/287.05;density=density/A(:,tempPos);A(:,windPos)=A(:,windPos)*power(density/densityNormal,1/3);

MATLAB ERRORS GENERATED:

Error using \
Out of memory. Type HELP MEMORY for your options.
Error in scaleWind (line 31)
density=density/A(:,tempPos);
Error in getLongData (line 19)
A = scaleWind(A,avgWind);
Advertisement

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.

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.

yes the './' operator is what i needed

Thanks

This topic is closed to new replies.

Advertisement