Can someone help implement the perfect shuffle function?

1 回表示 (過去 30 日間)
Kevin Junior
Kevin Junior 2013 年 10 月 15 日
回答済み: Jos (10584) 2013 年 10 月 19 日
function outString = perfectMShuffle(inString,m)
% This function shuffles the characters in the inString by picking 1st,
% m+1st, 2m+1st characters till it reaches the end, then picking 2nd, m+2nd
% 2m+2nd etc.
% Examples:
% perfectMShuffle('abcdef',3) -> 'adbecf'
% perfectMShuffle('abcdef',2) -> 'acebdf'
Here is what I have so far, I might be totally wrong but thats what I tried
N = length(inString)/2;
y = zeros(N,1);
for k = 1:N
y(2*k-1) = m;
y(2*k) = N+k;
end

回答 (1 件)

Jos (10584)
Jos (10584) 2013 年 10 月 19 日
x = 'ABCDEFGHIJKL'
m = 3
% the following line pre-allocates an output to store things in, makes things faster!
y = x ;
y(:) = '.' % for display purposes only
N = numel(x)
for k=1:m-1
% there are m-1 positions where we have to begin in x
ix = k:m:N % indices into X
iy = (1:numel(ix)) + (k-1) % indices into y
y(iy) = x(ix) % copy
end

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by