Check and replace elements in a matrix
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Konstantinos
le 9 Jan 2015
Commenté : Konstantinos
le 9 Jan 2015
I have a random generated matrix (2x8) which consist of zeros and ones. How can I check, in each row, if there are 2 ones in the first 4 elements and 1 one in the last 4 elements ? Moreover, If there are more ones than the prescribed, I would like to exchange the redundants ones with zeros.
i.e rand = ( 1 1 1 0 0 1 1 0; 0 1 1 1 0 1 0 0 )
modified_rand = ( 1 1 0 0 0 1 0 0; 0 1 1 0 0 1 0 0 )
Any help could be useful. Thanks in advance !
0 commentaires
Réponse acceptée
Azzi Abdelmalek
le 9 Jan 2015
a=[ 1 1 1 0 0 1 1 0; 0 1 1 1 0 1 0 0 ]
a1=a(:,1:4);
a2=a(:,5:end)
a1(cumsum(a1,2)>2)=0
a2(cumsum(a2,2)>1)=0
b=[a1 a2]
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!