# (Matlab) Multiply whole array?

This topic is 2032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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); Edited by suliman

##### 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 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 on other sites

yes the './' operator is what i needed

Thanks

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009325
• ### Who's Online (See full list)

There are no registered users currently online

×