how to randomize a vector as fast as possible

2 ビュー (過去 30 日間)
eden kisos
eden kisos 2019 年 12 月 8 日
編集済み: JESUS DAVID ARIZA ROYETH 2019 年 12 月 8 日
hello;
I have this vector:
0.001:0.001:125 (size 125000)
I would like to shuffle it as fast as possible because this function repeates alot in my code (without using randperm and such because it is really slow, I dont have the shuffle function on my computer)
any ideas?
thanks :)

回答 (2 件)

Stijn Haenen
Stijn Haenen 2019 年 12 月 8 日
this is finished in 28 sec, i dont know how fast other methods are.
tic
Vec=1:125000;
numelVec=numel(Vec);
new_Vec=zeros(1,numelVec);
for i=1:numelVec
rand_num=datasample(Vec,1);
Vec(Vec==rand_num)=[];
new_Vec(i)=rand_num;
end
toc
  3 件のコメント
eden kisos
eden kisos 2019 年 12 月 8 日
the loop is too slow for me. I am doing it more that 1000000 times. randperm is even slower
i tried this way but it is still runs slow:
jump= 0.001:0.001:125
[~,randomly]=sort(rand(1,length(jump),1),2);
[~,order]= sort (randomly);
jump=jump(order);
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 8 日
this is much faster than what you currently have:
jump= 0.001:0.001:125
jump=jump(randperm(125000));

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


JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 8 日
I recommend using a MEX file https://ww2.mathworks.cn/matlabcentral/fileexchange/27076-shuffle?s_tid=FX_rc3_behav if you want to do the process much faster, here you can download this file and add it to your folder, do mex Shuffle.c for once on your matlab console, and you can now use a function much faster than randperm , you could compare it as follows:
Vec=1:125000;
tic
shuflle=Vec(randperm(125000));%normal
toc
tic
shuflle2=Shuffle(Vec);%with mex file
toc
  2 件のコメント
eden kisos
eden kisos 2019 年 12 月 8 日
I tried to use this function and this is the warnning I get: Need compiled mex file!
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 8 日
編集済み: JESUS DAVID ARIZA ROYETH 2019 年 12 月 8 日
first of all you need run mex Shuffle.c in your matlab command window
mex Shuffle.c
and if you don't have a supported compiler then you need to install it following the instructions that matlab gives you in the error

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

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by