Conditional Skip in for loop

조회 수: 2 (최근 30일)
Sarah Aimen
Sarah Aimen 2019년 7월 10일
답변: Image Analyst 2019년 7월 10일
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,

채택된 답변

Matt J
Matt J 2019년 7월 10일
편집: Matt J 2019년 7월 10일
Apply the suggestions in this thread to A=logical(x)+1.
  댓글 수: 4
Matt J
Matt J 2019년 7월 10일
Sarah's comment relocated here
Perfect! Works very well!
Thank you very much, Matt!
Matt J
Matt J 2019년 7월 10일
You're welcome. Please Accept-click the answer, though, to certify that we solved it.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 7월 10일
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)

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by