Generating a "special" probability random matrix in efficient way

1 回表示 (過去 30 日間)
nedo nodo
nedo nodo 2012 年 11 月 30 日
Hi,
I would to generate a probability random matrix with two constraints: the first is that the sum of the values of the row must be equal to 1 and the second is that one element of each row has to be a value greater than 0.5. I have written this:
if true
for l=1:n
while (nnz(random_data(l,:)) < d)
m=randi(d,1,1);
if (random_data(l,m)==0)
row_sum=sum(random_data(l,:));
if(row_sum == 0)
random_data(l,m)=0.5+0.5*rand(1,1);
else
if(nnz(random_data(l,:))==(d-1))
random_data(l,m)=1-row_sum;
else
random_data(l,m)=(1-row_sum)*rand(1,1);
end
end
end
end
end
end
It is very inefficient. Can you help me?

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 11 月 30 日
編集済み: Azzi Abdelmalek 2012 年 11 月 30 日
try this
n=10,m=12;
x=rand(n,m)/m;
[~,id1]=max(x');
id2=m+1-id1;
for k=1:n
x(k,id1(k))=0.5;
x(k,id2(k))=1-sum(x(k,:))+x(k,id2(k))
end

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by