Change the first half of consecutive 1 in a column into 2

1 vue (au cours des 30 derniers jours)
Ana Maria Alzate
Ana Maria Alzate le 18 Mai 2021
Commenté : Matt J le 19 Mai 2021
I have a matrix with 31 rows and around 500 columns. The matrix has NaN, 0s and 1s. The 1s are always consecutive in each column, but the number is not always the same. There are also some columns without 1s.
I need to find the 1s in each column, count them and calculate the half, then I ned to change the first half into 2s, but the second half of 1s should stay as 1s.

Réponse acceptée

Matt J
Matt J le 18 Mai 2021
Modifié(e) : Matt J le 18 Mai 2021
map=(A==1);
C=cumsum(map,1,'omitnan');
A( C<=C(end,:)/2 & map) = 2;
  2 commentaires
Ana Maria Alzate
Ana Maria Alzate le 18 Mai 2021
Thank you! The lines work perfect.
Just one question, right now, if the column have an odd number of 1s, it changes less than the half to 1 and it would be better if it is the opposite. For example, if there are 9 number 1, it is better 5 2s and 4 1s, but now I am getting 4 1s and 5 2s.
Matt J
Matt J le 19 Mai 2021
Just use ceil().

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by