Home > Archive > Matlab > May 2005 > optimize for loops
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
optimize for loops
|
|
|
| Hi!
I have some for loops which i'd like to optimize.
The first one is activating or not some edit fields which tags are:
Edit11, Edit12, ... Edit99.
The loop looks like this:
%---------------------------------------------------
for i=1:9
for j=1:9
edit_tag = ['Edit' int2str(i) int2str(j)];
set(findobj('tag',edit_tag), 'Enable', 'off');
end;
end;
%---------------------------------------------------
I need to use it many times with different i and j values (1:9, 4:6,
3:7 etc.)
The second one is more complicated and it's working like a filter. I
need to calculate new value of the pixel using values of pixels which
are around successive pixels. If i have a big image, this solution
takes a lot of time.
%--------------------------------------------------------
Mask = [0 -1 0; -1 5 -1; 0 -1 0]
Sum = 0;
Mult = 0;
k=1;
m=1;
ImageOut = ImageIn;
for x=2:ImageIn_Max_X-1 %loop for the image
for y=2:ImageIn_Max_Y-1
for i=x-1:x+1 %loop for the neighbour of the pixel
for j=y-1:y+1
Mult = ImageIn(i,j) * (Mask(k,m));
Sum = Sum + Mult;
m=m+1;
end; %for j
m=1;
k=k+1;
end; %for i
k=1;
ImageOut(x,y) = Sum;
Sum = 0;
end; %for y
end; %for x
%---------------------------------------------------------
So is there a way to speed up this loops????
I'll be grateful for any help.
| |
| Titus Edelhofer 2005-05-31, 4:05 pm |
| Hi,
before going deeper into removing the for loops I'd suggest a matrix to
store
your handles (or even better, store in a matrix during creation)
editHandles = zeros(9);
for i=1:9
for j=1:9
edit_tag = ['Edit' int2str(i) int2str(j)];
editHhandles(i,j) = findobj('tag',edit_tag);
end;
end;
Later just call
set(editHandles(1:5,1:6), 'enable', 'off')
Titus
"Dwade" <dwade@o2.pl> schrieb im Newsbeitrag
news:ef079a8.-1@webx.raydaftYaTP...
> Hi!
> I have some for loops which i'd like to optimize.
>
> The first one is activating or not some edit fields which tags are:
> Edit11, Edit12, ... Edit99.
> The loop looks like this:
>
> %---------------------------------------------------
> for i=1:9
> for j=1:9
> edit_tag = ['Edit' int2str(i) int2str(j)];
> set(findobj('tag',edit_tag), 'Enable', 'off');
> end;
> end;
> %---------------------------------------------------
>
> I need to use it many times with different i and j values (1:9, 4:6,
> 3:7 etc.)
>
> The second one is more complicated and it's working like a filter. I
> need to calculate new value of the pixel using values of pixels which
> are around successive pixels. If i have a big image, this solution
> takes a lot of time.
>
> %--------------------------------------------------------
> Mask = [0 -1 0; -1 5 -1; 0 -1 0]
> Sum = 0;
> Mult = 0;
> k=1;
> m=1;
> ImageOut = ImageIn;
> for x=2:ImageIn_Max_X-1 %loop for the image
> for y=2:ImageIn_Max_Y-1
> for i=x-1:x+1 %loop for the neighbour of the pixel
> for j=y-1:y+1
> Mult = ImageIn(i,j) * (Mask(k,m));
> Sum = Sum + Mult;
> m=m+1;
> end; %for j
> m=1;
> k=k+1;
> end; %for i
> k=1;
> ImageOut(x,y) = Sum;
> Sum = 0;
> end; %for y
> end; %for x
> %---------------------------------------------------------
>
> So is there a way to speed up this loops????
> I'll be grateful for any help.
| |
| Brett Shoelson 2005-05-31, 4:05 pm |
| "Dwade" <dwade@o2.pl> wrote in message news:ef079a8.-1@webx.raydaftYaTP...
> Hi!
> I have some for loops which i'd like to optimize.
>
> The first one is activating or not some edit fields which tags are:
> Edit11, Edit12, ... Edit99.
> The loop looks like this:
>
> %---------------------------------------------------
> for i=1:9
> for j=1:9
> edit_tag = ['Edit' int2str(i) int2str(j)];
> set(findobj('tag',edit_tag), 'Enable', 'off');
> end;
> end;
> %---------------------------------------------------
>
> I need to use it many times with different i and j values (1:9, 4:6,
> 3:7 etc.)
>
> The second one is more complicated and it's working like a filter. I
> need to calculate new value of the pixel using values of pixels which
> are around successive pixels. If i have a big image, this solution
> takes a lot of time.
>
> %--------------------------------------------------------
> Mask = [0 -1 0; -1 5 -1; 0 -1 0]
> Sum = 0;
> Mult = 0;
> k=1;
> m=1;
> ImageOut = ImageIn;
> for x=2:ImageIn_Max_X-1 %loop for the image
> for y=2:ImageIn_Max_Y-1
> for i=x-1:x+1 %loop for the neighbour of the pixel
> for j=y-1:y+1
> Mult = ImageIn(i,j) * (Mask(k,m));
> Sum = Sum + Mult;
> m=m+1;
> end; %for j
> m=1;
> k=k+1;
> end; %for i
> k=1;
> ImageOut(x,y) = Sum;
> Sum = 0;
> end; %for y
> end; %for x
> %---------------------------------------------------------
>
> So is there a way to speed up this loops????
> I'll be grateful for any help.
Yeah, that first loop is in desperate need of attention. Consider:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%[colo
r=darkred]
buttontags =
'tag4'
'tag3'
'no2'
'tag1'
[color=darkred]
matches =
1
1
0
1
[color=darkred]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Should make short work of it.
I don't have time to play with your filter problem at the moment, but I'd
suggest you consider using the filter function. Read the help for filter2
and conv2.
Cheers,
Brett
--
char(cumsum(...
[32 83 -11 7 -10 7 7 -4 -1 -46 40 -3 7 -3 15 -74 64 -5 -1 -58 57 8 7 -86
0]))
| |
|
| Brett Shoelson wrote:
>
>
> "Dwade" <dwade@o2.pl> wrote in message
> news:ef079a8.-1@webx.raydaftYaTP...
> are:
> 4:6,
> filter. I
> which
> solution
>
>
> Yeah, that first loop is in desperate need of attention. Consider:
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> buttontags =
>
> 'tag4'
> 'tag3'
> 'no2'
> 'tag1'
>
>
> matches =
>
> 1
> 1
> 0
> 1
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> Should make short work of it.
>
> I don't have time to play with your filter problem at the moment,
> but I'd
> suggest you consider using the filter function. Read the help for
> filter2
> and conv2.
>
> Cheers,
> Brett
>
> --
> char(cumsum(...
> [32 83 -11 7 -10 7 7 -4 -1 -46 40 -3 7 -3 15 -74 64 -5 -1 -58 57 8
> 7 -86
> 0]))
>
>
>
Thank You very much guys!!! The first problem is solved. About the
second i'll read about filter2 and conv2 functions.
|
|
|
|
|