How to spread out an arry of matrix randomly

2 ビュー (過去 30 日間)
Hamid Reza Barzegar
Hamid Reza Barzegar 2016 年 5 月 18 日
I have random matrix like this
[2 2 2 2 1 1 1 1 1 -1 -1 -1 -1 -1]
and depend to the result of calculation number of 2 1 -1 will be different but i want to spread these numbers randomly like this e.g. [2 1 2 -1 1 1 2 -1 2 -1 -1 2 ]
how should i do this?

回答 (2 件)

Walter Roberson
Walter Roberson 2016 年 5 月 18 日
YourVector(randperm(length(YourVector)))
  2 件のコメント
Hamid Reza Barzegar
Hamid Reza Barzegar 2016 年 5 月 18 日
first=100;
Second=50;
Third=50;
Alloc_Sc=[2*ones(1,first),ones(1,Second),-1*ones(1,Third)];
Alloc_Sc2=Alloc_Sc(length(randperm(Alloc_Sc)));
it shows the error:
Error using randperm Size inputs must be scalar.
Stephen23
Stephen23 2016 年 5 月 19 日
@Hamid Reza Barzegar: Walter Roberson's answer clearly uses randperm(length(...)). You have written length(randperm(...).

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


Roger Stafford
Roger Stafford 2016 年 5 月 18 日
編集済み: Roger Stafford 2016 年 5 月 18 日
Let n be the desired length of the result and let a = [2 1 -1] :
p = randi(3,n,1); % <-- Corrected
v = a(p(randperm(n))); % v is your result % <-- Corrected
  5 件のコメント
Roger Stafford
Roger Stafford 2016 年 5 月 18 日
As I state above, your expression is in error in using "length(randperm(Alloc_Sc))" instead of "randperm(length(Alloc_Sc))".
Hamid Reza Barzegar
Hamid Reza Barzegar 2016 年 5 月 19 日
Thanks for your reply i changed as you mentioned but the data is returned is number between 1 to 200 not my data now there is no error but not my data
first=100;
Second=50;
Third=50;
Alloc_Sc=[2*ones(1,first),ones(1,Second),-1*ones(1,Third)];
Alloc_Sc2=randperm(length(Alloc_Sc));

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

カテゴリ

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