speed up this function

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

sundus
sundus 2015년 6월 5일
편집: Walter Roberson 2015년 6월 5일
% 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

답변 (2개)

Jan
Jan 2015년 6월 2일
편집: Jan 2015년 6월 2일

0 개 추천

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

sundus
sundus 2015년 6월 5일
Hi Jan, with this answer, all the elements of RKey will be the same value: which is 132 that means the position of the smallest element.what I need is the position for all the elements from the smallest to the largest. thank you.
Walter Roberson
Walter Roberson 2015년 6월 5일
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 2015년 6월 5일
편집: per isakson 2015년 6월 5일
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 2015년 6월 5일

0 개 추천

function [RKey] = Shuffle1R( Key1 )
[~, ~, RKey] = unique(Key1);
end

댓글 수: 1

sundus
sundus 2015년 6월 5일
Thank you Walter, you are right, so I send the code with the loop.

이 질문은 마감되었습니다.

태그

질문:

2015년 6월 2일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by