Filter löschen
Filter löschen

Can someone help implement the perfect shuffle function?

1 Ansicht (letzte 30 Tage)
Kevin Junior
Kevin Junior am 15 Okt. 2013
Beantwortet: Jos (10584) am 19 Okt. 2013
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

Antworten (1)

Jos (10584)
Jos (10584) am 19 Okt. 2013
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

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by