find first time 0 again after higher 0

1 回表示 (過去 30 日間)
Frederik Reese
Frederik Reese 2022 年 6 月 17 日
コメント済み: Star Strider 2022 年 6 月 19 日
Hi,
probably a simple question, but How can I find the Value (index) in each row of a matrix, which is after some values higher than 0 is 0 again?
for example this row in a matrix:
0 0 0 0.0619999999999550 0.108000000000004 0.129999999999995 0.156999999999982 0.176999999999964 0.185000000000002 0.189999999999998 0.187999999999988 0.187999999999988 0.185999999999979 0.182999999999993 0.180999999999983 0.176999999999964 0.170999999999992 0.164999999999964 0 0
I want to know the index of this row at the big black 0.
Thanks

採用された回答

Star Strider
Star Strider 2022 年 6 月 17 日
One approach —
M = [0 0 0 0.0619999999999550 0.108000000000004 0.129999999999995 0.156999999999982 0.176999999999964 0.185000000000002 0.189999999999998 0.187999999999988 0.187999999999988 0.185999999999979 0.182999999999993 0.180999999999983 0.176999999999964 0.170999999999992 0.164999999999964 0 0 ]
M = 1×20
0 0 0 0.0620 0.1080 0.1300 0.1570 0.1770 0.1850 0.1900 0.1880 0.1880 0.1860 0.1830 0.1810 0.1770 0.1710 0.1650 0 0
idx = M~=0; % Logical Index
Out = strfind(idx, [1 0])+1 % Desired Index
Out = 19
Check = M((-1:1)+Out) % Check Result
Check = 1×3
0.1650 0 0
Thius should be reasonably robust to similar problems.
.
  2 件のコメント
Frederik Reese
Frederik Reese 2022 年 6 月 19 日
Thanks forr your help.
If i put a for loop on it to find all the values in different rows the for loop stops when it hits a row with only 0 values. Do you know how to solve this problem?
for i=1:29
idx = D_HQ5000_WH(i,:)~=0; % Logical Index
Out = strfind(idx, [1 0])+1 % Desired Index
% Check = D_HQ5000_WH((-1:1)+Out) % Check Result
Zeit_Flutende (i,1) = Out
end
Star Strider
Star Strider 2022 年 6 月 19 日
As always, my pleasure!
The only approach that comes quickly to mind is to add an if, elseif, else block —
D_HQ5000_WH = [randi([0 1], 1, 10); zeros(1,10); randi([0 1], 1, 10); ones(1,10)]
D_HQ5000_WH = 4×10
1 1 1 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1
for i = 1:size(D_HQ5000_WH,1)
idx = D_HQ5000_WH(i,:)~=0; % Logical Index
Out = NaN;
if any(idx) & ~all(idx)
Out = strfind(idx, [1 0])+1; % Desired Index
end
Check = Out % Delete Later
end
Check = 1×2
4 6
Check = NaN
Check = 1×2
3 9
Check = NaN
I assigned ‘Out’ to NaN if either ‘idx’ has all the elements true or all the elements false. This traps conditions where all the elements are zero or none are zero, and sets those results to NaN. (It does not trap situations where there is more than one transition, so I assume that never occurs in practise.)
.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by