From a matrix how can I randomly select one column combination at a time

4 ビュー (過去 30 日間)
Neetha Francis
Neetha Francis 2021 年 5 月 13 日
コメント済み: Neetha Francis 2021 年 5 月 15 日
Suppose I have
A = [ 1 0 0
0 1 1
1 1 0]
I would like to get any column based on value of a random number, and send to another matrix with the other two columns successively.

回答 (1 件)

Image Analyst
Image Analyst 2021 年 5 月 13 日
編集済み: Image Analyst 2021 年 5 月 13 日
This will do it:
A = [ 1 0 0
0 1 1
1 1 0]
[rows, columns] = size(A)
% Get a random column.
randomColumn = randi(columns)
% Get indexes of the other columns.
otherColumns = setdiff(1:columns, randomColumn)
% Take that random column, and tack on the other columns to the right of it.
outputMatrix = A(:, [randomColumn, otherColumns])
For example:
A =
1 0 0
0 1 1
1 1 0
randomColumn =
2
otherColumns =
1 3
outputRowVector =
0 1 0
1 0 1
1 1 0

カテゴリ

Help Center および File ExchangeTime Series についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by