From: someone on
"Bryan " <bryan.kim(a)navy.mil> wrote in message <i158n0$eov$1(a)fred.mathworks.com>...
> Avoid using the for loop if you can. One way to avoid the for loop is to vectorize your code. See the following example.
>
> % good programming practice, but not desirable MATLAB programming.
> myEvaluate= [];
> cnt = 1;
> for i = 1:1:length(myArray) %myArray length = 50,000
> if sensorfail(i) ~= 1
> x_in(cnt) = x(i);
> y_in(cnt) = y(i);
> myEvaluate (cnt) = 1;
> cnt = cnt + 1;
> end
> end
>
> % Optimized MATLAB code
> myEvaulate = 1:1:length(myArray)
> myEvaulate = myEvaulate(sensorfail ~= 1)
> x_in(cnt) = x(myEvaulate);
> y_in(cnt) = y(myEvaulate);
>
> You will see enhanced performance if you run tic; ExampleCode; toc; .
>
> The problem is not your CPU speed or the 'tsearchn' function. Your 'for' loop is the performance dragging culprit.

I don't believe the above is a totally accurate statement. Much of the "performance dragging culprit" is probably due to the fact that the arrays are not preallocated. I have seen instances where for loops run faster than vectorized code. It depends on the size of the arrays and the amount of memory available to MATLAB.

If you cannot avoid, then make it to use smaller index values by using cell arrays.
>
>
> "Sang-yeop Chung" <cesar419(a)gmail.com> wrote in message <i14923$riq$1(a)fred.mathworks.com>...
> > Hi,
> >
> > I run my own matlab code which contains 'for' loop and 'tsearchn' function.
> > It works well, but the speed is too slow. I want to speed up my process
> > without fixing my code.
> > I use Dual core 2.13GHz cpu.
> >
> > first,
> > I use Dual core 2.13GHz cpu.
> > Is there any function to use dual core cpu at the same time??
> > In other word, I want to use my whole dual core cpu for the process.
> >
> > second,
> > I find 'maxNumcompthreads' funtion, but it doen't work after checking code
> > run time by 'tic-toc'.
> > And someone at google report about 'GPUmat'.
> > I wonder that I can use it.
> >
> > if anyone can answer my question, I need your helps.
> >
> > Regards.
> >
> > Chung
From: Matt Fig on
"Bryan " <bryan.kim(a)navy.mil> wrote in message <i158n0$eov$1(a)fred.mathworks.com>...
> Avoid using the for loop if you can. One way to avoid the for loop is to vectorize your code. See the following example.
>
> % good programming practice, but not desirable MATLAB programming.
> myEvaluate= [];
> cnt = 1;
> for i = 1:1:length(myArray) %myArray length = 50,000
> if sensorfail(i) ~= 1
> x_in(cnt) = x(i);
> y_in(cnt) = y(i);
> myEvaluate (cnt) = 1;
> cnt = cnt + 1;
> end
> end
>
> % Optimized MATLAB code
> myEvaulate = 1:1:length(myArray)
> myEvaulate = myEvaulate(sensorfail ~= 1)
> x_in(cnt) = x(myEvaulate);
> y_in(cnt) = y(myEvaulate);
>
> You will see enhanced performance if you run tic; ExampleCode; toc; .
>
> The problem is not your CPU speed or the 'tsearchn' function. Your 'for' loop is the performance dragging culprit. If you cannot avoid, then make it to use smaller index values by using cell arrays.


Seconding someone,

Without seeing the actual code, there is no way you to know if the FOR loops are actually the bottleneck or not. As shown many times on this NG, a FOR loop can be faster than the best vectorized solution found. Even if the FOR loops are the bottleneck, there are instances where a vectorization cannot be found, or where a vectorized solution runs the system out of memory.