Re: How do I optimize the code to make it faster?



Roger Stafford wrote:


In article <ef33782.-1@xxxxxxxxxxxxxxxx>, Niner
<ninerh@xxxxxxxxxxx> wrote:

Hi,

I have a question about the 'loop' operation. I have a big
matrix,
and I want to perform the calculation several times. I use this
code
to do the job.
-------------------------------
k=1;
for i=0.1:0.1:50
c = i*d;
A = c*B./C;
PL(:,k)=A';
k=k+1;
end
-------------------------------
The code above shows that when given matrix B and C, I can get
matrix
A, then I assign the matrix A to the matrix PL as a column. For
example, B and C are 1-D matrix, usually 1x484. c is the
constant. So
the A should be the matrix of size 1x484. Then I assign it to
matrix
PL as one column: PL(:,k)=A'. Since i=0.1:0.1:50, there are 500
columns in PL. And the PL has size of 484*500. The purpose of
the
code is when given a 'c', there are 484 values resulted from
the
'c'.


My question is, how can I do to perform the calculation without
using
for-loop? I notice the speed is way too slow if the size of
matrix B
and C is increasing, and if the range i is increasing. I know
that if
I only use the 'matrix way', like A=c*B./C;, to calculate, the
Matlab
just runs in a flip. So I am asking if anyone knows how to
accelerate
the operation like this code?

Any feedback is welcome. Thank you very much for your help.

Niner
-------------------------
PL = (B./C).'*[0.1*d:0.1*d:50*d];

(Remove "xyzzy" and ".invalid" to send me email.)
Roger Stafford


Thanks a lot, Roger. You solved my question. Much appreciate. But
here I have another similar question. This time the matrix is using
add/minus sign.

A = d*i + B./C % d is a constant, and i=0.1:0.1:50

The result matrix PL still has size of 484x500. For a given i, I can
get a matrix of 1x484. Now I have i ranging from 0.1 to 50. How can I
use the same concept you mentioned in this calculation? Thanks a lot
for your help.

Niner
.



Relevant Pages

  • Re: create a list of items with pre-specified numbers
    ... Assume 'numbers' is a column vector of positive integers. ... The quantity 'Item' is not needed in this calculation. ... Roger Stafford ...
    (comp.soft-sys.matlab)
  • Re: create a list of items with pre-specified numbers
    ... The quantity 'Item' is not needed in this calculation. ... Roger Stafford ... Oops! ... and 2) that Bruno is way ahead of me. ...
    (comp.soft-sys.matlab)
  • Re: calculation speed up
    ... > Is there some way to speed up this calculation? ... PHI = exp; ... Roger Stafford ... Prev by Date: ...
    (comp.soft-sys.matlab)
  • Re: Non-rectangular 3D plot
    ... loop' properly to get x and y vectors and the z matrix. ... Roger Stafford wrote: ... rectangular space. ... There is an example of a sphere generated by 'surf' in the function reference ...
    (comp.soft-sys.matlab)
  • Re: vectorize
    ... ellieandrogerxyzzy@xxxxxxxxxxxxxxxxxxxxxx (Roger Stafford) wrote: ... I believe your code is slow because for each pass through the loop, ... possibly a way of vectorizing the entire process. ... positive integers and all like numbers are grouped together, ...
    (comp.soft-sys.matlab)