How to find out point of change of sequence of ones and zeros?

1 回表示 (過去 30 日間)
Lukas Poviser
Lukas Poviser 2021 年 7 月 27 日
コメント済み: Lukas Poviser 2021 年 7 月 28 日
Hello,
I have this matrice:
1 NaN 3.28000000000000
1 NaN 3.32000000000000
1 NaN 3.36000000000000
1 NaN 3.40000000000000
0 0.0320040000000000 3.44000000000000
0 0.0697230000000000 3.48000000000000
0 0.124815600000000 3.52000000000000
0 0.158877000000000 3.56000000000000
and I would like to have another column where would be just the number where the first column changes from 1 to 0.
Result should be like this:
1 NaN 3.28000000000000
1 NaN 3.32000000000000
1 NaN 3.36000000000000
1 NaN 3.40000000000000 3.40000000000000
0 0.0320040000000000 3.44000000000000
0 0.0697230000000000 3.48000000000000
0 0.124815600000000 3.52000000000000
0 0.158877000000000 3.56000000000000

採用された回答

Jan
Jan 2021 年 7 月 27 日
編集済み: Jan 2021 年 7 月 28 日
What do you expect in the other rows of the 4th column? All rows must have the same number of columns, according to the definition of a matrix. If zeros are fine:
index = find(A(:, 1) == 0, 1) - 1; % [EDITED, -1 appended]
if any(index)
A(index, 4) = A(index, 3);
end
  5 件のコメント
Jan
Jan 2021 年 7 月 28 日
Now you post another input, but do not mention, what you expect as output.
  • If you still want to use the first change from 1 to 0, use the code of my answer.
  • If you want to find all changes from 1 to 0:
index = strfind(A(:, 1).', [1, 0]);
B = A(index, 3);
% Alternative:
index = diff(A(:,1)) < 0;
Lukas Poviser
Lukas Poviser 2021 年 7 月 28 日
It works perfectly! Thanks!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by