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

1 vue (au cours des 30 derniers jours)
Lukas Poviser
Lukas Poviser le 27 Juil 2021
Commenté : Lukas Poviser le 28 Juil 2021
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

Réponse acceptée

Jan
Jan le 27 Juil 2021
Modifié(e) : Jan le 28 Juil 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 commentaires
Jan
Jan le 28 Juil 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;
Lukas Poviser
Lukas Poviser le 28 Juil 2021
It works perfectly! Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Operators and Elementary Operations dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by