To choose and shift an element randomly

3 visualizaciones (últimos 30 días)
yue ishida
yue ishida el 2 de En. de 2012
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.

Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 2 de En. de 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

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by