how to setup matrix with random unique combinations

4 ビュー (過去 30 日間)
Pablo
Pablo 2011 年 5 月 23 日
Hello, in a M = 4x4 matrix I want to place 1 unique value (x=1) per column so that sum(M,2) = 1: e.g., [1 0 0 0; 0 0 0 1; 0 1 0 0; 0 0 1 0 ]
However the combination has to be randomly made.
I can use randi(4,1) to set up the first column, but the subsequent columns will have to be constrained following the first column and using "while" doesn't seem to help. Sugestions?

採用された回答

Fangjun Jiang
Fangjun Jiang 2011 年 5 月 23 日
a=eye(4);
b=randperm(4);
c=a(b,:)
  1 件のコメント
Pablo
Pablo 2011 年 5 月 23 日
Thank you, this works well!

サインインしてコメントする。

その他の回答 (1 件)

Laura Proctor
Laura Proctor 2011 年 5 月 23 日

If the columns and rows must all sum to 1, then try this:

M = eye(4);
M = M(:,randperm(4));

カテゴリ

Help Center および File ExchangeError Functions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by