フィルターのクリア

How to count the maximum and minimum range of non-zeros elements?

1 回表示 (過去 30 日間)
Jahanzeb Saqib
Jahanzeb Saqib 2020 年 12 月 7 日
コメント済み: Jahanzeb Saqib 2020 年 12 月 8 日
let's suppose we have A of length 50 rows
how we can calculate the maximum range of non-zeros element and as well as minimum
A = [0 0 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 ]
In this case Max and Min should be,
Max = 5
Min = 2
Is there a straightforward solution to do this? without using complex looping because in my case data is too large approximately length of A is 220079705, your kind consideration will be highly regarded by all means,
Many Thanks
  2 件のコメント
hangar011
hangar011 2020 年 12 月 7 日
There are built-in functions for this...
length(find(A)) or length(find(A==0))
Image Analyst
Image Analyst 2020 年 12 月 7 日
Sorry, but length(find()) won't work. See my answer below.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 12 月 7 日
Use regionprops(), in the Image Processing Toolbox:
A = [0 0 1 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 ]
props = regionprops(logical(A), 'Area');
allLengths = [props.Area];
minLength = min(allLengths)
maxLength = max(allLengths)
  1 件のコメント
Jahanzeb Saqib
Jahanzeb Saqib 2020 年 12 月 8 日
Thank you so much!
The code is working as expected and to the point!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by