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

Thread Subject:
improve loop performance

Subject: improve loop performance

From: vmpli

Date: 28 Aug, 2013 18:22:09

Message: 1 of 4

Hi Guys,

I look for ways to improve a loop. Simplified part of the code:

fl = cell(KK,1);
for kk = 1:KK
    fl{kk} = NaN(10);
    for ix = 1:10
        for iy = 1:10
            [~,fl{kk}(ix,iy)] = function();
        end
    end
end

Each cell of fl contains a 10x10 double array. For KK above some fifty or hundred, the loop gets very time-consuming.

The "function" is a toolbox function (not distributed with ML) which I can't modify. Anyway, I just wonder if there are some ways how to otherwise arrange the loop or split the problem in smaller portions. I looked in the docs how to programm more efficiently - parfor, vectorization, etc.. There are some examples, but I don't see what of those techniques is relevant to my problem and how I could apply it. So any hints are very appreciated. Thank you.

Subject: improve loop performance

From: IceHaven

Date: 28 Aug, 2013 18:32:09

Message: 2 of 4

"vmpli" wrote in message <kvlf4g$cu3$1@newscl01ah.mathworks.com>...
> Hi Guys,
>
> I look for ways to improve a loop. Simplified part of the code:
>
> fl = cell(KK,1);
> for kk = 1:KK
> fl{kk} = NaN(10);
> for ix = 1:10
> for iy = 1:10
> [~,fl{kk}(ix,iy)] = function();
> end
> end
> end
>
> Each cell of fl contains a 10x10 double array. For KK above some fifty or hundred, the loop gets very time-consuming.
>
> The "function" is a toolbox function (not distributed with ML) which I can't modify. Anyway, I just wonder if there are some ways how to otherwise arrange the loop or split the problem in smaller portions. I looked in the docs how to programm more efficiently - parfor, vectorization, etc.. There are some examples, but I don't see what of those techniques is relevant to my problem and how I could apply it. So any hints are very appreciated. Thank you.

so absolutely nothing is getting passed to the function? does that mean all the cell members will have the same value?

Subject: improve loop performance

From: TideMan

Date: 28 Aug, 2013 20:13:19

Message: 3 of 4

On Thursday, August 29, 2013 6:32:09 AM UTC+12, IceHaven wrote:
> "vmpli" wrote in message <kvlf4g$cu3$1@newscl01ah.mathworks.com>...
>
> > Hi Guys,
>
> >
>
> > I look for ways to improve a loop. Simplified part of the code:
>
> >
>
> > fl = cell(KK,1);
>
> > for kk = 1:KK
>
> > fl{kk} = NaN(10);
>
> > for ix = 1:10
>
> > for iy = 1:10
>
> > [~,fl{kk}(ix,iy)] = function();
>
> > end
>
> > end
>
> > end
>
> >
>
> > Each cell of fl contains a 10x10 double array. For KK above some fifty or hundred, the loop gets very time-consuming.
>
> >
>
> > The "function" is a toolbox function (not distributed with ML) which I can't modify. Anyway, I just wonder if there are some ways how to otherwise arrange the loop or split the problem in smaller portions. I looked in the docs how to programm more efficiently - parfor, vectorization, etc.. There are some examples, but I don't see what of those techniques is relevant to my problem and how I could apply it. So any hints are very appreciated. Thank you.
>
>
>
> so absolutely nothing is getting passed to the function? does that mean all the cell members will have the same value?

And why are you using a cell array?
Why not a numeric array:
ff=NaN(10,10,KK);

But as IceHaven intimates, it entirely comes down to what the arguments for function() are and whether it returns only a scalar, in which case there is no hope of vectorisation and you're stuck with the loops.
But if it can return a vector or (even better) a matrix, you will be able to vectorise.

Subject: improve loop performance

From: vmpli

Date: 30 Aug, 2013 10:39:08

Message: 4 of 4

Thank you Gurus for replies. I have not come across more complicated tasks so far, so I have much to learn about effective programming.

Yes I should better use numeric arrays where the dimensions enable that.

The output argument to the "function()" is a scalar, the input argument is a symbolic model object which is re-parametrised within the loop (I didn't show those non-Matlab commands). From what you write, I think it will not be possible to use the vectorization technique.

So I think about splitting this task in smaller portions. KK is a known scalar and I also know that for KK some 10 or 15 the loop passes quite well. However, the only way I could think of was writing an outer loop that would call the inner loop in smaller portions as until reaching KK. No idea how to avoid another loop...

I also read about commands like pause() as I don't need any exact times. Though I have no idea which of these directions to go.

The overall computing time might be rather long, but at least it would not load the processor so heavily (so e.g. execution could be stopped quickly by Ctrl+C, no risk of freezing...) For my purpose this would be a sufficient improvement. Thank you for hints.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us