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 Center および File ExchangeGeometric 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