Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Dwade
05-31-05 09:05 PM


Re: optimize for loops
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Titus Edelhofer
05-31-05 09:05 PM


Re: optimize for loops
"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'
 

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]))



Report this thread to moderator Post Follow-up to this message
Old Post
Brett Shoelson
05-31-05 09:05 PM


Re: optimize for loops
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Dwade
05-31-05 09:05 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Matlab archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:37 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.