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

조회 수: 2 (최근 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 CenterFile Exchange에서 Operators 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