From: Daphne on

I am running a small simulation program that I wrote, changing parameters for each run, doing some analysis, and saving into an excel file. Since there are many parameters, I end up with 7 loops running within eachother...
My program is basically an m-file with several inputs, which I choose in the loops...

Heres an example of what it may look like.
for rate = [ 5, 15, 30, 60, 90, 120 ]
for a = 1e-9*[ 10, 25, 50, 100, 200, 300, 400, 500 ]
for divide_nr_by = [ 1000, 1550, 2200, 2900, 3650, 4550 ]
for window = [ 10, 15 ]
for perm = [1:20]
% run some analysis
end end end end end

Is there any way to somehow vectorize this even partially?
I would be happy to get rid of some of the loops, but still need all the conditions to run.

Another question, when I do run the loops, they initially run pretty fast, but then slow down. I don't carry any variables with me, but write it all into a file, and each matrix gets run over. Why would the slowdown occur?

Thanks!
Daphne
From: Jan Simon on
Dear Daphne,

> Heres an example of what it may look like.
> for rate = [ 5, 15, 30, 60, 90, 120 ]
> for a = 1e-9*[ 10, 25, 50, 100, 200, 300, 400, 500 ]
> for divide_nr_by = [ 1000, 1550, 2200, 2900, 3650, 4550 ]
> for window = [ 10, 15 ]
> for perm = [1:20]
> % run some analysis
> end end end end end

The FOR loops itself cannot be vectorized.
You describe the interesting part of your program by "% run some analysis". We definitely need much more details.

Jan
From: James Tursa on
"Daphne" <daphnew_too_nospam(a)yahoo.com> wrote in message <i27lak$l25$1(a)fred.mathworks.com>...
>
> Another question, when I do run the loops, they initially run pretty fast, but then slow down. I don't carry any variables with me, but write it all into a file, and each matrix gets run over. Why would the slowdown occur?

Usually the result of increasing an array size within the loop(s). Can't say for sure unless we see your code.

James Tursa
From: someone on
"Daphne" <daphnew_too_nospam(a)yahoo.com> wrote in message <i27lak$l25$1(a)fred.mathworks.com>...
>
> I am running a small simulation program that I wrote, changing parameters for each run, doing some analysis, and saving into an excel file. Since there are many parameters, I end up with 7 loops running within eachother...
> My program is basically an m-file with several inputs, which I choose in the loops...
>
> Heres an example of what it may look like.
> for rate = [ 5, 15, 30, 60, 90, 120 ]
> for a = 1e-9*[ 10, 25, 50, 100, 200, 300, 400, 500 ]
> for divide_nr_by = [ 1000, 1550, 2200, 2900, 3650, 4550 ]
> for window = [ 10, 15 ]
> for perm = [1:20]
> % run some analysis
> end end end end end
>
> Is there any way to somehow vectorize this even partially?

It MAY be possible to completely vectorize it and remove ALL the for loops (which doesn't necessarily mean it will run any faster). But from what little information you gave (% run some analysis), its impossible for ANYONE to tell.

> I would be happy to get rid of some of the loops, but still need all the conditions to run.
>
> Another question, when I do run the loops, they initially run pretty fast, but then slow down. I don't carry any variables with me, but write it all into a file, and each matrix gets run over. Why would the slowdown occur?

Are you writting to the same or different files? If you unnecessarily keep the files open between writes/loops, that COULD be part of the problem.

Its usually because vectors or matricies aren't properly preallocated. But, again, given what little information you provided, its impossible to tell.

Can you provide a SMALL sample of your code to show whats going on inside the for loops?

>
> Thanks!
> Daphne
From: Daphne on

Sorry for being so vague. I really didn't know where to start, as in the loops there are about 400 lines of code in the main function + about 5 subfunctions (the simulation, image procesing, iterations on various parameters and other goodies).
I guess what I would like to do is not vectorize the code itself (I've vectorized as much as I could think of), but perhaps find a way to reduce the number of loops needed to send the parameters list into the main subfunction. I'm guessing that's not possible.
I really don't know what to put here, and can't put the whole code (length and reveal).
I do make sure to preallocate and try to clear any unnecessary variables between runs, I also use the ~ for unneeded variables.

What the function does is basically is collect the parameters from the loops, call the main subfunction and generate an image according to specifications, then I run image processing procedures on a thresholded image (a previously determined threshold), compare the processed image to an original to find quality (another time-consuming bit, small enough to post so here it is)

A = matrix;
B = estimated_matrix;
W = weight_factor;
Q = W *(sum(sum(and( A, B)))/sum(sum( A))) + ...
(1-W)*(sum(sum(and(1-A,1-B)))/sum(sum(1-A)));

Once good quality is obtained, many parameters are calculated and saved into a file.
Sorry I can't be more specific...

About the files, yes, I open (fopen) one file in the begining and use fprintf to write the line of final data to it after each run. Perhaps it would be better to just collect it all into a matrix and dump it to a file every few hundreds of lines (~60 columns worth of numerical data each run).

dlmwrite (as opposed to fprintf) doesn't require to keep the file open, does it?

Daphne