array operation in matlab
Show older comments

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.
Accepted Answer
More Answers (2)
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)
% or:
% number of unique elements greater than 770:
nnz(unique(FF_liter_ho_) > 770)
3 Comments
ali hassan
on 12 Feb 2022
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])
end_idx = strfind(idx,[true false])
n_consecutive = end_idx-start_idx
max_n_consecutive = max(n_consecutive)
Image Analyst
on 12 Feb 2022
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
on 11 Feb 2022
sum(Table.FF_liter_ho_>770)
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!