Can someone help implement the perfect shuffle function?

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by