MATLAB Answers

0

Conditional Skip in for loop

Asked by Sarah Aimen on 10 Jul 2019
Latest activity Answered by Image Analyst
on 10 Jul 2019
Hello,
I have a data array like:
x = [0, 0, 45, 43, 42, 41, 0 , 0 , 0, 0, 45, 43, 42, 41 ];
In my data points, 2 times my data became non-zero. Which I call them an "event".
I want my code to calculates the number of events as "2".
and calculate the sume of numbers at each event.
I'd appreciate your help at this matter.
Thanks,

  0 Comments

Sign in to comment.

Tags

2 Answers

Matt J
Answer by Matt J
on 10 Jul 2019
Edited by Matt J
on 10 Jul 2019
 Accepted Answer

Apply the suggestions in this thread to A=logical(x)+1.

  4 Comments

Show 1 older comment
Matt J
on 10 Jul 2019
Just use a while loop, then:
x = [1, 2, 45, 43, 42, 41, 6 , 2, 7];
i=0;
while i<length(x)
i=i+1;
if x(i) > 10;
x(i)
i = i + 3;
end
end
Matt J
on 10 Jul 2019
Sarah's comment relocated here
Perfect! Works very well!
Thank you very much, Matt!
Matt J
on 10 Jul 2019
You're welcome. Please Accept-click the answer, though, to certify that we solved it.

Sign in to comment.


Answer by Image Analyst
on 10 Jul 2019

Try this (requires the Image Processing Toolbox, which you probably already have):
x = [0, 0, 45, 43, 42, 41, 0 , 0 , 0, 0, 45, 43, 42, 41 ];
% Count number of non-zero regions: (requires Image Processing Toolbox)
[~, numNonZeroRegions] = bwlabel(x)
% Compute sum of values
props = regionprops(x>0, x, 'PixelValues');
sumOfValues = sum(vertcat(props.PixelValues), 2)

  0 Comments

Sign in to comment.