Jump to content

  • Log In with Google      Sign In   
  • Create Account

(Matlab) Multiply whole array?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 suliman   Members   -  Reputation: 569

Like
0Likes
Like

Posted 25 March 2013 - 07:29 AM

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, 25 March 2013 - 08:57 AM.


Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8605

Like
1Likes
Like

Posted 25 March 2013 - 07:36 AM

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.



#3 cadjunkie   Members   -  Reputation: 1352

Like
0Likes
Like

Posted 25 March 2013 - 08:20 AM

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.



#4 suliman   Members   -  Reputation: 569

Like
0Likes
Like

Posted 26 March 2013 - 02:00 AM

yes the './' operator is what i needed

Thanks






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS