How can I keep values of an array random.
显示 更早的评论
I have an array T=(0 1 2 7 71 158 187).
i wanna generate A=(158 71 2 187 0 152 1)
please tell me a command for generating A.
回答(2 个)
madhan ravi
2018-12-29
编辑:madhan ravi
2018-12-29
Note : In your desired result A there appears a number 152 instead of 7 which I guess is perhaps a typo.
Just use random indexing using randperm() or randsample() so that the indices are unique not repeated :
A = T(randperm(numel(T)))
%or
A = T(randsample(numel(T),numel(T)))
Will just leave this here : (Reason: People shouldn't think I edited my answer after the comments below so here is the proof)


2 个评论
Akira Agata
2018-12-29
If you allow duplication, that's ok. But if not, I would recommend using randperm function.
>> T = [0,1,2,7,71,158,187];
>> A = T(randperm(numel(T)))
A =
1 187 158 0 7 71 2
类别
在 帮助中心 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!