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:
Is it possible to vectorize a for loop with if statements?

Subject: Is it possible to vectorize a for loop with if statements?

From: Darryl

Date: 8 Aug, 2010 16:47:05

Message: 1 of 5

Hi,
when i used the tic toc function, it says that every iteration of the code below took around 5 seconds. I would very much like to speed up the process if possible using vectorized code. Is that at all possible? Thanks

Code:

tic
for i = 1:ROW
    for j = 1:COL
    
        if handles.ListImages(PicNum).imgLeafArea(i,j) == 0
            LeafArea = LeafArea + 1;
              if handles.ListImages(PicNum).imgDiseaseArea(i,j) == 0
                DiseaseArea = DiseaseArea + 1;
              end
        end
    end
end

fltPercent = (DiseaseArea/LeafArea)*100

time = toc

Subject: Is it possible to vectorize a for loop with if statements?

From: Jan Simon

Date: 8 Aug, 2010 17:30:22

Message: 2 of 5

Dear Darryl,

> for i = 1:ROW
> for j = 1:COL
> if handles.ListImages(PicNum).imgLeafArea(i,j) == 0
> LeafArea = LeafArea + 1;
> if handles.ListImages(PicNum).imgDiseaseArea(i,j) == 0
> DiseaseArea = DiseaseArea + 1;
> end
> end
> end
> end
> fltPercent = (DiseaseArea/LeafArea)*100

Leaf = handles.ListImages(PicNum).imgLeafArea(1:Row, 1:Col);
Disease = handles.ListImages(PicNum).imgDiseaseArea(1:Row, 1:Col);
% or if the dimensions are already matching:
% Leaf = handles.ListImages(PicNum).imgLeafArea;
% Disease = handles.ListImages(PicNum).imgDiseaseArea;
zeroLeaf = Leaf(:) == 0
LeafArea = sum(zeroLeaf);
DiseaseArea = sum(and(zeroLeaf, Disease(:) == 0));
fltPercent = (DiseaseArea/LeafArea)*100;

Kind regards, Jan

Subject: Is it possible to vectorize a for loop with if statements?

From: Roger Stafford

Date: 8 Aug, 2010 17:33:23

Message: 3 of 5

"Darryl " <battousai.kenshee@gmail.com> wrote in message <i3mn29$32n$1@fred.mathworks.com>...
> Hi,
> when i used the tic toc function, it says that every iteration of the code below took around 5 seconds. I would very much like to speed up the process if possible using vectorized code. Is that at all possible? Thanks
>
> Code:
>
> tic
> for i = 1:ROW
> for j = 1:COL
>
> if handles.ListImages(PicNum).imgLeafArea(i,j) == 0
> LeafArea = LeafArea + 1;
> if handles.ListImages(PicNum).imgDiseaseArea(i,j) == 0
> DiseaseArea = DiseaseArea + 1;
> end
> end
> end
> end
>
> fltPercent = (DiseaseArea/LeafArea)*100
>
> time = toc
- - - - - - - - - - - -
  It can easily be vectorized. You will have to see if that helps the timing.

tic
LeafArea = sum(handles.ListImages(PicNum).imgLeafArea(:)==0);
DiseaseArea = sum(handles.ListImages(PicNum).imgLeafArea(:)==0 & ...
                  handles.ListImages(PicNum).imgDiseaseArea(:)==0);
fltPercent = DiseaseArea/LeafArea*100;
time = toc

Roger Stafford

Subject: Is it possible to vectorize a for loop with if statements?

From: ImageAnalyst

Date: 8 Aug, 2010 18:09:56

Message: 4 of 5

It appears that your disease image is zero in places other than where
you have leaves, hence the if's in your code and the and's in Jan and
Roger's code. Any of that code should work but I'm wondering why you
want a disease image that has disease in places other than the
leaves? Does it also identify disease on areas classified as stems or
trunks? If not, and teh disease exists only within the leaf areas,
then you can do away with and'ing with the leaf image and just work
directly on the disease image to speed it up even more.

If it does have disease on other areas then you might want to separate
the classes into different images just to clarify and avoid confusion
leafDiseaseImage = not(diseaseImage) & not(leafImage);
stemDiseaseImage = not(diseaseImage) & not(stemImage);
trunkDiseaseImage = not(diseaseImage) & not(trunkImage);
You'd have to do this anyway if you ever wanted to display or analyze
the stem and trunk diseased regions.

Subject: Is it possible to vectorize a for loop with if statements?

From: Darryl

Date: 9 Aug, 2010 01:25:05

Message: 5 of 5

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <3a845205-9d1b-4a51-9141-692ffe07e647@u26g2000yqu.googlegroups.com>...
> It appears that your disease image is zero in places other than where
> you have leaves, hence the if's in your code and the and's in Jan and
> Roger's code. Any of that code should work but I'm wondering why you
> want a disease image that has disease in places other than the
> leaves? Does it also identify disease on areas classified as stems or
> trunks? If not, and teh disease exists only within the leaf areas,
> then you can do away with and'ing with the leaf image and just work
> directly on the disease image to speed it up even more.
>
> If it does have disease on other areas then you might want to separate
> the classes into different images just to clarify and avoid confusion
> leafDiseaseImage = not(diseaseImage) & not(leafImage);
> stemDiseaseImage = not(diseaseImage) & not(stemImage);
> trunkDiseaseImage = not(diseaseImage) & not(trunkImage);
> You'd have to do this anyway if you ever wanted to display or analyze
> the stem and trunk diseased regions.
----------------------------------------------------------------------------------------------------------------------

Thank you everyone for the quick replies! It really did speed up the program, it now takes only 0.0093 seconds per iteration. Amazing.

@ImageAnalyst
I wish i knew how to write codes that good =/ Unfortunately my algorithm detects random particles outside the leaf. That's why i resorted to such. Thanks anyway, maybe when i get good at it, i'll be able to do that.

I know it's off-topic, but anyone has experience with what functions, codes,books to read, etc, would be recommended for detecting leaf diseases? Maybe it can help improve my code. Thanks.

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