フィルターのクリア

How to randomly exchange 2 entries in a column?

2 ビュー (過去 30 日間)
Grace
Grace 2014 年 10 月 1 日
回答済み: Star Strider 2014 年 10 月 1 日
Hi, I have
a=[ 1 2;
3 4;
2 1;
4 5;
5 3]
I want to choose randomly any 2 entries from a column and exchange them. For example, I randomly choose entries 2 and 5 from the second column, exchange these two entries to become
b=[ 1 5;
3 4;
2 1;
4 2;
5 3]
How am I going to make this? Thanks in advance.

採用された回答

Star Strider
Star Strider 2014 年 10 月 1 日
This seems to work:
[r,c] = size(a); % ‘a’ Row, Col Size
cc = randi(c); % Choose Random Column
rc = randi(r, 1, 2); % Choose Random Rows
b = a; % Create ‘b’
b(rc,cc) = a(fliplr(rc), cc) % Swap Rows of Chosen Column

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by