speed up this function
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
function [RKey] = Shuffle1R( Key1 )
% Using algorithm to specify the key back for shuffling
stime=tic;
% Key1=[162,28,62,124,78,14,116,133,164,141,109,160,84,16,60,11,32,119,49,150,26,118,77,42,8,115,161,56,123,106,75,98,130,79,155,55,151,167,57,95,45,93,101,46,65,40,64,68,125,52,66,12,3,86,128,135,108,19,145,166,85,156,27,90,17,33,91,157,168,29,34,63,96,18,4,58,67,153,111,104,131,51,94,73,154,83,43,149,54,10,59,39,97,158,140,137,53,38,120,21,92,129,107,47,37,148,82,71,41,36,7,143,61,152,139,165,113,48,9,87,100,74,126,22,69,99,102,20,121,163,169,1,142,81,103,159,112,76,70,72,13,144,25,146,138,24,5,30,6,2,114,23,35,44,122,134,136,80,127,15,88,89,50,132,31,147,110,105,117];
% k=size(Key1,2);
RKey=zeros(1,0)*zeros(0,size(Key1,2));
[~,Ind ]=min(Key1(:));
RKey(:)=Ind;
Key1(Ind)=10000;
toc(stime);
end
1 commentaire
sundus
le 5 Juin 2015
Modifié(e) : Walter Roberson
le 5 Juin 2015
Réponses (2)
function [RKey] = Shuffle1R( Key1 )
[~, Ind] = min(Key1(:));
RKey(1:size(Key1, 2)) = Ind;
end
Notes:
- zeros(1,0)*zeros(0,size(Key1,2)) is worse than zeros(1, size(Key1, 2))
- Key1(Ind)=10000 can be omitted
3 commentaires
sundus
le 5 Juin 2015
Walter Roberson
le 5 Juin 2015
Your code to be sped up has no loop to find the ordering of the elements, it only does the min() once. It assigns the result to RKey(:) which means to write to all elements. Jan's answer replicates that behavior. If you want a different behavior to be sped up you will need to supply that code.
sundus
le 5 Juin 2015
Modifié(e) : per isakson
le 5 Juin 2015
Walter Roberson
le 5 Juin 2015
function [RKey] = Shuffle1R( Key1 )
[~, ~, RKey] = unique(Key1);
end
1 commentaire
sundus
le 5 Juin 2015
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!