To choose and shift an element randomly

3 次查看(过去 30 天)
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.

采纳的回答

Andrei Bobrov
Andrei Bobrov 2012-1-2
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 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by