Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Optimization of a simple IF-FOR loop

Asked by Mohsen on 18 Jun 2013

How can I optimize the following code so that it runs faster?

    for i=1:iNZ;
        iPointsinSlice=1;
        for j=1:iNCUr;
            if UrContourZ(j)==ZSlice(i);
                iPointsinSlice = iPointsinSlice+1;
                UrPointX(iPointsinSlice-1)=1 + (UrContourX(j)-Offset(1,1));
                UrPointY(iPointsinSlice-1)=1 + (UrContourY(j)-Offset(2,1));
            end
        end
    end

0 Comments

Mohsen

Products

1 Answer

Answer by Elliot Olsen on 18 Jun 2013

Hi Mohsen,

A few things I will recommend:

1. Take advantage of Matlab's Profiler. Type profview in the Command Window to access it. From here you can run code and see what lines are taking the most time to run, etc. Profiler can also give you hints on how to optimize your code.

2. I don't know what your full code looks like, but in this code snippet you presented, you do not initialize the arrays URPointX or URPointY. Prior to the for loop, you should initialize them like this:

URPointX = zeros(maxRowsX, maxColumnsX);
URPointY = zeros(maxRowxY, maxColumnsY);

This way, the arrays are allocated in memory and do not have to be reallocated every loop iteration since they don't change size.

3. Here are two links for general code performance improvement for Matlab. Vectorization is very important.

Techniques for Improving Perfomance

Vectorization

If you want more specific help, please post a full code that runs.

1 Comment

Mohsen on 18 Jun 2013

Hi Elliot,

Thank you for your answer.

1. I am using Profiler in order to optimize the code. 2. I have pre-allocated the variables that change in size during the for loop. 3. Here is the code. Any idea about how to optimize the code?

iNZ=11; iNCUr=55;

UrPointX=zeros(iNCUr);

UrPointY=zeros(iNCUr); UrContourZ=[ 0 0 0 0 0 -5 -5 -5 -5 -5 -10 -10 -10 -10 -10 -10 -15 -15 -15 -15 -15 -15 -20 -20 -20 -20 -20 -20 -25 -25 -25 -25 -30 -30 -30 -30 -30 -30 -35 -35 -35 -35 -35 -35 -40 -40 -40 -40 -40 -40 -45 -45 -45 -45 -45]; ZSlice=[ 5 0 -5 -10 -15 -20 -25 -30 -35 -40 -45];

UrContourX=[ 69.103 70.633 71.05 70.911 68.964 69.938 68.408 69.103 69.938 70.633 67.713 68.686 69.659 70.007 70.285 69.52 67.018 68.269 69.381 69.938 69.103 68.964 69.729 67.713 67.921 68.547 69.868 70.216 70.494 68.408 68.825 70.355 70.633 70.702 70.007 68.964 68.2 68.964 70.911 69.52 69.52 67.991 69.103 70.077 70.077 69.242 68.478 69.173 70.772 70.772 70.633 68.964 69.242 70.633 70.633];

UrContourY=[ 44.801 45.914 47.026 47.026 46.887 50.919 49.946 48.556 48.417 49.112 52.24 50.711 50.641 50.989 51.754 53.144 54.534 52.866 52.866 54.395 55.369 55.369 55.994 55.508 54.187 53.352 53.422 55.508 56.62 56.481 53.839 53.978 54.117 55.438 56.272 56.272 55.786 53.7 54.256 55.925 56.064 55.09 53.561 53.839 53.492 53.422 52.935 51.614 52.24 52.588 51.058 50.78 49.668 50.224 50.919];

Offset=[ 6.8947; -16.648; 5];

    for i=1:iNZ;
        iPointsinSlice=1;
        for j=1:iNCUr;
            if UrContourZ(j)==ZSlice(i);
                iPointsinSlice = iPointsinSlice+1;
                UrPointX(iPointsinSlice-1)=1 + (UrContourX(j)-Offset(1,1));
                UrPointY(iPointsinSlice-1)=1 + (UrContourY(j)-Offset(2,1));
            end
        end
    end
Elliot Olsen

Contact us