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
댓글 수: 0
답변 (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
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!