array operation in matlab

1 回表示 (過去 30 日間)
ali hassan
ali hassan 2022 年 2 月 11 日
コメント済み: Image Analyst 2022 年 2 月 13 日
here we have a vector column 'FF_liter_ho_'.If i want to know that how many times non zero values were greater than 770 in a row.
so it happened only once in the column.

採用された回答

Image Analyst
Image Analyst 2022 年 2 月 12 日
Try this:
FF_liter_ho_ = [NaN 773.5 NaN NaN NaN NaN NaN NaN NaN 773.5, 771, 888, NaN 813, 889, 550, 345, NaN]
% Create mask of elements greater than 770
mask = (FF_liter_ho_ > 770)
% Count how many times there are 1 or more such numbers "in a row" (consecutively).
% User says "i want to know that how many times non zero values were greater than 770 in a row."
props = regionprops(mask, 'Area');
runLengths = [props.Area]
% Find the longest run
maxRunLength = max(runLengths)
You get:
mask =
1×18 logical array
0 1 0 0 0 0 0 0 0 1 1 1 0 1 1 0 0 0
runLengths =
1 3 2
maxRunLength =
3
Note: when you said "non-zero" values, I took it to mean "both non-zero and non-nan" values, in other words, just the valid numbers.
  2 件のコメント
ali hassan
ali hassan 2022 年 2 月 13 日
@Image Analyst it can be done in a number of ways like by using loops, movsum, conv etc
but i did not come across this method. its working perfectly.
but how do this region prop actually works?
how it detects the repeated number of values?
Image Analyst
Image Analyst 2022 年 2 月 13 日
@ali hassan, it uses a concept called Connected Component labeling:
Basically it finds all elements that are connected together -- they have continguous pixels or elements. Then for each separate connected group it assigns a label. Then regionprops() makes the requested measurements on each labeled group and returns the measurements for all the groups as a structure array, though it can also return a table if you ask it to. For a 2-D array, the 'Area' is the number of pixels, while if the image is 1-D vector, the 'Area' measurement really is the length of the region in number of elements.

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

その他の回答 (2 件)

Voss
Voss 2022 年 2 月 11 日
FF_liter_ho_ = [NaN 773.5 NaN NaN NaN NaN NaN NaN NaN 773.5];
% number of elements greater than 770:
nnz(FF_liter_ho_ > 770)
ans = 2
% or:
% number of unique elements greater than 770:
nnz(unique(FF_liter_ho_) > 770)
ans = 1
  3 件のコメント
Voss
Voss 2022 年 2 月 12 日
I see. When you said "in a row", I interpreted that to mean "in a given row of the matrix" or "by row" rather than "consecutively", which is the meaning you intended.
% FF_liter_ho_ = [NaN 773.5 NaN NaN NaN 760 NaN NaN NaN NaN 773.5];
FF_liter_ho_ = [771 773.5 NaN NaN NaN 760 NaN NaN 776 780 773.5];
idx = [false FF_liter_ho_(:).' > 770 false];
start_idx = strfind(idx,[false true])
start_idx = 1×2
1 9
end_idx = strfind(idx,[true false])
end_idx = 1×2
3 12
n_consecutive = end_idx-start_idx
n_consecutive = 1×2
2 3
max_n_consecutive = max(n_consecutive)
max_n_consecutive = 3
Image Analyst
Image Analyst 2022 年 2 月 12 日
Personally I think the regionprops() method in my Answer is more straightforward since you're using a function specifically meant for computing this, and don't have to do it yourself by manually manipulating indexes, but this method is okay if you do not have the Image Processing Toolbox.

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


Enrico Gambini
Enrico Gambini 2022 年 2 月 11 日
sum(Table.FF_liter_ho_>770)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by