Info

This question is closed. Reopen it to edit or answer.

speed up this function

1 view (last 30 days)
sundus
sundus on 2 Jun 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comment
sundus
sundus on 5 Jun 2015
Edited: Walter Roberson on 5 Jun 2015
% function [RKey] = Shuffle1R( Key1 )
% Using algorithm to specify the key back for shuffling
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];
stime=tic;
RKey=zeros(1,numel(Key1));
for i=1:numel(Key1),
[j,Ind ]=min(Key1(:));
RKey(i)=Ind;
Key1(Ind)=10000;
end
RKey
toc(stime);
% end

Answers (2)

Jan
Jan on 2 Jun 2015
Edited: Jan on 2 Jun 2015
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 Comments
Walter Roberson
Walter Roberson on 5 Jun 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
sundus on 5 Jun 2015
Edited: per isakson on 5 Jun 2015
Hi Walter, this is the code with the loop:
% function [RKey] = Shuffle1R( Key1 )
% Using algorithm to specify the key back for shuffling
%
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];
%
stime=tic;
RKey=zeros(1,numel(Key1));
for i=1:numel(Key1),
[j,Ind ]=min(Key1(:));
RKey(i)=Ind;
Key1(Ind)=10000;
end
RKey
toc(stime);
% end

Walter Roberson
Walter Roberson on 5 Jun 2015
function [RKey] = Shuffle1R( Key1 )
[~, ~, RKey] = unique(Key1);
end
  1 Comment
sundus
sundus on 5 Jun 2015
Thank you Walter, you are right, so I send the code with the loop.

Community Treasure Hunt

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

Start Hunting!