To choose and shift an element randomly
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a matrix as below:
a=[11 11 22 22 33 33; 22 22 33 33 44 44; 44 44 11 11 22 22]
a=
11 11 22 22 33 33
22 22 33 33 44 44
44 44 11 11 22 22
Then, I need to do matrix operation as algorithm below:
1. In row 1, I need to choose an element randomly. 2. I need to shift the element with next row shift. For example, if 22 is chosen, it need to shift with 33. So, it will become like this:
b=
11 11 33 22 33 33
22 22 22 33 44 44
44 44 11 11 22 22
3. I will do this operation for next rows too.
Therefore, I need any help to code this problem.
0 commentaires
Réponse acceptée
Andrei Bobrov
le 2 Jan 2012
try this is code:
a=[11 11 22 22 33 33; 22 22 33 33 44 44; 44 44 11 11 22 22];
k = randi(size(a,2),size(a,1)-1,1);
b = a;
for j1 = 1:size(a,1)-1
b([j1,j1+1],k(j1)) = b([j1+1,j1],k(j1));
end
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB 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!