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

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Jan
Jan am 27 Jul. 2021
Bearbeitet: Jan am 28 Jul. 2021
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 Kommentare
Jan
Jan am 28 Jul. 2021
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;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Operators and Elementary Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by