Filter löschen
Filter löschen

keep first time a value appear in a colomn and replace following ones

1 Ansicht (letzte 30 Tage)
Dam
Dam am 29 Mai 2015
Beantwortet: Jos (10584) am 29 Mai 2015
Good evening, Supposing I have the following matrix A= 5*3 =[1 1 1; 1 1 1; 1 0 1 ; 1 0 0; 1 0 0] What i want to do is to keep the first zero of each colomn and to replace following zeros by 1, so that i get a matrix B =5*3= [1 1 1; 1 1 1; 1 0 1 ; 1 1 0; 1 1 1]
Thank you in advance

Antworten (2)

Roger Stafford
Roger Stafford am 29 Mai 2015
B = (A==0);
B = A + (B & cumsum(B)>1);

Jos (10584)
Jos (10584) am 29 Mai 2015
This will keep the first zero in each row of A and replace every following zero with one:
A(A==0 & cumsum(A==0,2)>1) = 1

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by