# Can someone help implement the perfect shuffle function?

4 ビュー (過去 30 日間)
Kevin Junior 2013 年 10 月 15 日

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',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

#### 0 件のコメント

サインイン to comment.

### 回答 (1 件)

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

#### 0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。

Translated by