How to find the lowest and highest rows in a column vector which contain a value.
1 次查看(过去 30 天)
显示 更早的评论
So, I have a 20x1 vector (A) shown below. How would I find the row number for the lowest and highest row that contain a value? In the vector below the lowest would be row 6, and the highest would be row 17. Thanks.
A =
NaN
NaN
NaN
NaN
NaN
1
1
1
NaN
NaN
NaN
1
1
1
NaN
NaN
1
NaN
NaN
NaN
0 个评论
采纳的回答
Akira Agata
2018-1-31
idx = ~isnan(A);
lowestRow = min(find(idx));
highestRow = max(find(idx));
5 个评论
Jos (10584)
2018-1-31
You also forgot the for-loop counter
for K = lowestRow:highestRow
if ~isnan(A(K))
disp(K)
end
end
However,
tf = ~isnan(A)
disp( A(tf) )
would almost do the same and is much more efficient
更多回答(1 个)
Jos (10584)
2018-1-31
idx = ~isnan(A);
lowestRow = find(idx, 1, 'first')
highestRow = find(idx, 1, 'last')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!