serpind.m

버전 1.0.0.0 (2.29 KB) 작성자: Kenneth Johnson
serpentine traversal of an N-dimensional array
다운로드 수: 593
업데이트 날짜: 2016/3/31

라이선스 보기

function [p,ip,m] = serpind(s)
% Return serpentine indexing list for array size s.
%
% syntax:
% p = serpind(s);
% [p,ip] = serpind(s);
% [p,ip,m] = serpind(s);
%
% input arg:
%
% s (size-[1,N] array of non-negative integers): array size
%
% output args:
%
% p (size-[M,1] array of non-negative integers; M = prod(s)): flat indexing list for
% serpentine array traversal (p is a permutation of (1:M).'.)
%
% ip (size-[M,1] array of non-negative integers; optional output): inverse permutation
% of p
%
% m (size-[M,N] array of non-negative integers; optional output): N-dimensional array
% subscripts corresponding to p
%
% Each element p(j) corresponds to a multidimensional array subscript list m(j,1:N)
% defined by
% [m(j,1), m(j,2), ... m(j,N)] = ind2sub(s,p(j))
% The subscript lists m(1,:), m(2,:), ... traverse the set of all indices for a size-s
% array (i.e., 1 <= m(j,k) <= s(k) for each j = 1:M, k = 1:N). The "serpentine"
% traversal order has the property that m(j,:) and m(j+1,:) differ in only one dimension
% index, and the difference in that index is either +1 or -1.
%
% The index list p is a permutation of (1:M).', and its inverse is ip (i.e., p(ip) =
% (1:M).'). Thus, for an array A, the two sequential operations
% A = A(p);
% A = A(ip);
% are equivalent to
% A = A(:);
%
% The elements of a size-s array A can be sequenced in serpentine order as follows,
% s = size(A);
% [p,ip] = serpind(s);
% A = A(p);
% The original array can then be reconstructed as follows:
% A = reshape(A(ip),s);
%
% Version 04/09/2006
% Author: Kenneth C. Johnson
% software.kjinnovation.com
%
% See ZipInterp.pdf, Section 9.4 ("Seed chaining"), on the KJ Innovation website for an
% application example illustrating the use of serpind.m.
%
N = length(s);
M = prod(s);
p = zeros(M,1);
if M==0
if nargout>=2
ip = p;
if nargout>=3
m = zeros(0,N);
end
end
return
end
p(1) = 1;
len = 1;
stride = 1;
for k = 1:N
L = len;
for j=2:s(k)
p(len+1:len+L) = p(len:-1:len-L+1)+stride;
len = len+L;
end
stride = stride*s(k);
end
if nargout>=2
ip(p,1) = (1:M).';
if nargout>=3
[m{1:N}] = ind2sub(s,p);
m = cell2mat(m);
end
end

인용 양식

Kenneth Johnson (2024). serpind.m (https://www.mathworks.com/matlabcentral/fileexchange/10756-serpind-m), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2006a
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.0.0.0

Updated to add BSD License.