How to keep only a new modification in an array using for loop?
Afficher commentaires plus anciens
Hello, I have an array named 'a' cocsists of ones and zeros. What I want is
If an entry is zero , make it 1 and store the whole array, then move to the next term and if the elemnt is 1 store the array as it is and move to the next element in the array. I have tried in the code below but gives me a different answer. If anyone can help me in this.
a=[1 0 0 1 1 0 0 1 0];
i=1;
new=[];
for j=1:9
if a(i)==0
a(i)=1;
new(i,:)= a;
else
new(i,:)=a;
end
i=i+1
end
output of the above code
1 0 0 1 1 0 0 1 0
1 1 0 1 1 0 0 1 0
1 1 1 1 1 0 0 1 0
1 1 1 1 1 0 0 1 0
1 1 1 1 1 0 0 1 0
1 1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 1
What I want is
desired output=[1 0 0 1 1 0 0 1 0
1 1 0 1 1 0 0 1 0
1 0 1 1 1 0 0 1 0
1 0 0 1 1 0 0 1 0
1 0 0 1 1 0 0 1 0
1 1 1 1 1 1 0 1 0
1 0 0 1 1 0 1 1 0
1 0 0 1 1 0 0 1 0
1 0 0 1 1 0 0 1 1
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!