How to spread out an arry of matrix randomly
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
回答(2 个)
Walter Roberson
2016-5-18
YourVector(randperm(length(YourVector)))
2 个评论
Stephen23
2016-5-19
@Hamid Reza Barzegar: Walter Roberson's answer clearly uses randperm(length(...)). You have written length(randperm(...).
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
2016-5-18
As I state above, your expression is in error in using "length(randperm(Alloc_Sc))" instead of "randperm(length(Alloc_Sc))".
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!