create array

13 ビュー (過去 30 日間)
Mate 2u
Mate 2u 2011 年 5 月 9 日
Hi considering I want to make a a 3x6624 matrix of random numbers (>0 and <1) but I want the sum of each column of the3 numbers to add up to 1. Any help?
Thanks

採用された回答

Paulo Silva
Paulo Silva 2011 年 5 月 9 日
a=zeros(3,6624);
for b=1:size(a,2)
c(1)=rand/3;
c(2)=rand/3;
c(3)=1-c(1)-c(2);
cr=randperm(3);
a(:,b)=[c(cr(1));c(cr(2));c(cr(3))];
end
  1 件のコメント
Mate 2u
Mate 2u 2011 年 5 月 9 日
Is there anyway to do it faster without a loop?

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

その他の回答 (3 件)

Mate 2u
Mate 2u 2011 年 5 月 9 日
Is there a way to do this without a loop? So it could perform faster?

Sean de Wolski
Sean de Wolski 2011 年 5 月 9 日
A = rand(3,6624);
A = bsxfun(@rdivide,A,sum(A,1));
Also look at Roger's randfixedsum on the FEX.

Teja Muppirala
Teja Muppirala 2011 年 5 月 9 日
Paulo's algorithm without a loop:
a=zeros(3,6624);
a(1:2,:)=rand(2,6624)/3;
a(3,:)=1-sum(a);
P = randperm(6624*3);
[~,ord] = sort(ceil(P/3));
a(:) = a(P(ord));
figure
hist(a',.05:.1:.95);
Another possible algorithm:
a=zeros(3,6624);
a(1,:) = rand(1,6624);
a(2,:) = (1-sum(a)).*rand(1,6624);
a(3,:) = (1-sum(a)).*rand(1,6624);
a = bsxfun(@rdivide,a,sum(a));
P = randperm(6624*3);
[~,ord] = sort(ceil(P/3));
a(:) = a(P(ord));
figure
hist(a',.05:.1:.95);

カテゴリ

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