フィルターのクリア

How to produce data randomly from a given matrix without changing the dimension

3 ビュー (過去 30 日間)
Tinkul
Tinkul 2014 年 4 月 6 日
回答済み: Jos (10584) 2014 年 4 月 7 日
I have some data for eg
A=[1;3;5;7;2]
Now i have to produced all these data randomly. Is there any matlab command to produce these data randomly without changing the dimension...Please help...

採用された回答

Jan
Jan 2014 年 4 月 6 日
編集済み: Jan 2014 年 4 月 6 日
Perhaps you mean something like this:
A = [1;3;5;7;2]
for k = 1:10
A = A(randperm(length(A)))
end

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 4 月 6 日
Yes. It's a 5 row column vector. Of course it's possible to produce it, it just depends on what you mean by randomly. Do you just want to keep using randi(7,5) until your number match those? Like this:
A=[1;3;5;7;2]
% Use a Monte Carlo approach to generate A randomly
count = 1;
while count < 10000000
rv = randi(7, [5, 1]);
difference = abs(rv-A);
if max(difference) == 0
break;
end
count = count + 1;
end
message = sprintf('Found it after %d iterations', count);
uiwait(helpdlg(message));
Please clarify.
  2 件のコメント
Tinkul
Tinkul 2014 年 4 月 6 日
Suppose A=[1;3;5;7;2] I mentioned randomly, which means
A1=[1;7;3;5;2] or A1=[2;5;1;3;7] etc.etc.. That means the row of A1 matrix have the value which is also in A but in different row.
Image Analyst
Image Analyst 2014 年 4 月 7 日
This would have been helpful to include in your original post, don't you think? But it looks like Jan's code does what you want.

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


Jos (10584)
Jos (10584) 2014 年 4 月 7 日
Do you want a random permutation or a random derangement (i.e., none of the elements stays in place, as suggested by your comment to Image Analyst)?
% random permutation
B = A(randperm(numel(A))
% random derangement
B = A ;
if numel(A)>1
while any(B==A) % a rejection method
B = A(randperm(numel(A)) ;
end
else
disp('No derangement possible.')
end

カテゴリ

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

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by