To choose and shift an element randomly

3 ビュー (過去 30 日間)
yue ishida
yue ishida 2012 年 1 月 2 日
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 Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by