Thread Subject: for loop

Subject: for loop

From: Matlab User

Date: 19 Oct, 2009 23:46:03

Message: 1 of 5

Hi,
I am trying not to use for loop for my code, which is copied below, where bw is a binary image, and so is new_L. I am trying to get a new binary image new_ , which has value 1 at the location where both new_L and bw is 1.
Kindly help.
Thx.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for j=1:512
                  for k=1:512
                                if bw(k,j)==1 && new_L(k,j)==1
                                    new_(k,j)=1;
                                end
                   end
 end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Subject: for loop

From: ImageAnalyst

Date: 20 Oct, 2009 00:17:22

Message: 2 of 5

Use the bitand() function:
new_ = bitand(bw, new_L);
Or simply use logical operations
new_ = bw & new_L;

Subject: for loop

From: Matlab User

Date: 20 Oct, 2009 00:53:03

Message: 3 of 5

Thanks a lot ImageAnalysist,
But what if the code is something like below, where non of the 2D images are binary.
Thank you very much.

                       old_2D_Image;
                        for j=1:512
                            for k=1:512
                                if bw(k,j)>201 && new_L(k,j)>50
                                    new_(k,j)= old_2D_Image(k,j);
                                end
                            end
                        end

Subject: for loop

From: ImageAnalyst

Date: 20 Oct, 2009 01:16:15

Message: 4 of 5

On Oct 19, 8:53 pm, "Matlab User " <lovlov...@indiatimes.com> wrote:
> Thanks a lot ImageAnalysist,
> But what if the code is something like below, where non of the 2D images are binary.
> Thank you very much.
>
>                        old_2D_Image;
>                         for j=1:512
>                             for k=1:512
>                                 if bw(k,j)>201 && new_L(k,j)>50
>                                     new_(k,j)= old_2D_Image(k,j);
>                                 end
>                             end
>                         end

--------------------------------------------------------------------
Try something like this (untested):

new_ = zeros(size(bw));
indexes = bw>201 && new_L > 50;
new_(indexes(:)) = old_2D_Image(indexes(:));

Subject: for loop

From: Matt Fetterman

Date: 20 Oct, 2009 02:47:03

Message: 5 of 5


> --------------------------------------------------------------------
> Try something like this (untested):
>
> new_ = zeros(size(bw));
> indexes = bw>201 && new_L > 50;
> new_(indexes(:)) = old_2D_Image(indexes(:));

--

Or I could also suggest:
new=( bw>201 &new_L > 50).*old_2D_Image;

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com