1 |
function y=interlace(a,b,ind,dim) |
---|
2 |
%INTERLACE Insert Subarrays into Array. |
---|
3 |
% INTERLACE(A,B,IND,DIM) inserts the array B into array A after all indices |
---|
4 |
% given in the vector IND along dimension DIM. If DIM is not given, DIM is |
---|
5 |
% set equal to the first nonsingleton dimension of A. B and A must have the |
---|
6 |
% same class. Numeric, logical, and character classes are supported. B must |
---|
7 |
% have the same dimensions as A along all dimensions except DIM or B must |
---|
8 |
% be a scalar that is expanded to the correct size. When IND(k)=0, the |
---|
9 |
% array B is placed before the first index in A. |
---|
10 |
% |
---|
11 |
% INTERLACE(A,C,IND,DIM) where C is a cell array containing length(IND) |
---|
12 |
% cells, inserts the contents of the k-th cell C{k} after the k-th index |
---|
13 |
% IND(k). |
---|
14 |
% |
---|
15 |
% Examples: |
---|
16 |
% INTERLACE(ones(3,4),1:4,2) produces [1 1 1 1 |
---|
17 |
% 1 1 1 1 |
---|
18 |
% 1 2 3 4 |
---|
19 |
% 1 1 1 1] |
---|
20 |
% |
---|
21 |
% INTERLACE(ones(3,4),[5 6 7],[1 2 3],2) produces an error since B must be |
---|
22 |
% a column since DIM=2 is the column dimension |
---|
23 |
% |
---|
24 |
% INTERLACE(ones(3,4),[5;6;7],[1 2 3],2) produces [1 5 1 5 1 5 1 |
---|
25 |
% 1 6 1 6 1 6 1 |
---|
26 |
% 1 7 1 7 1 7 1] |
---|
27 |
% INTERLACE(ones(3,4),8,[0 3],1) produces [8 8 8 8 |
---|
28 |
% 1 1 1 1 |
---|
29 |
% 1 1 1 1 |
---|
30 |
% 1 1 1 1 |
---|
31 |
% 8 8 8 8] |
---|
32 |
% |
---|
33 |
% INTERLACE(ones(3,4),zeros(2,4),1) produces [1 1 1 1 |
---|
34 |
% 0 0 0 0 |
---|
35 |
% 0 0 0 0 |
---|
36 |
% 1 1 1 1 |
---|
37 |
% 1 1 1 1] |
---|
38 |
% |
---|
39 |
% INTERLACE(ones(3,4),{1:4, 4:-1:1},[1 3],1) produces [1 1 1 1 |
---|
40 |
% 1 2 3 4 |
---|
41 |
% 1 1 1 1 |
---|
42 |
% 1 1 1 1 |
---|
43 |
% 4 3 2 1] |
---|
44 |
% |
---|
45 |
% INTERLACE(ones(3,4),{[5;6;7],6,7},[1 1 3],2) produces [1 5 6 1 1 7 1 |
---|
46 |
% 1 6 6 1 1 7 1 |
---|
47 |
% 1 7 6 1 1 7 1] |
---|
48 |
% |
---|
49 |
% See also REPMAT, CAT |
---|
50 |
|
---|
51 |
% D.C. Hanselman, University of Maine, Orono, ME 04469 |
---|
52 |
% MasteringMatlab@yahoo.com |
---|
53 |
% Mastering MATLAB 7 |
---|
54 |
% 2006-02-19 |
---|
55 |
|
---|
56 |
if nargin<3 |
---|
57 |
error('interlace:NotEnoughInputArguments','Three or Four Inputs Required.') |
---|
58 |
end |
---|
59 |
asiz=size(a); % gather data about input A |
---|
60 |
aclass=class(a); |
---|
61 |
adims=ndims(a); |
---|
62 |
if nargin==3 |
---|
63 |
dim=find(asiz>1,1); |
---|
64 |
end |
---|
65 |
if isequal(aclass,'cell') || isequal(aclass,'struct') |
---|
66 |
error('interlace:ClassNotSupported','Cell and Struct Inputs Not Supported.') |
---|
67 |
end |
---|
68 |
if isempty(dim) || numel(dim)~=1 || fix(dim)~=dim || dim<1 || dim>adims |
---|
69 |
error('interlace:DimOutofRange','DIM Invalid.') |
---|
70 |
end |
---|
71 |
ind=ind(:); % make IND a vector |
---|
72 |
lind=length(ind); |
---|
73 |
N=asiz(dim); % length of A along chosen dimension |
---|
74 |
n=(1:N)'; % index along chosen DIM |
---|
75 |
asiz(dim)=1; |
---|
76 |
if isempty(ind) || any(ind>N|ind<0) |
---|
77 |
error('interlace:IndexOutofBounds','IND Value Out of Bounds.') |
---|
78 |
end |
---|
79 |
if iscell(b) % stretch input cell C to a column |
---|
80 |
c=b(:); |
---|
81 |
else % convert input B to cell for convenience |
---|
82 |
c=repmat({b},lind,1); |
---|
83 |
end |
---|
84 |
if length(c)~=lind |
---|
85 |
error('interlace:ArgumentMisMatch','Size of IND Must Match Size of C.') |
---|
86 |
end |
---|
87 |
notdim=true(1,adims); |
---|
88 |
notdim(dim)=false; % true for dimensions other than DIM |
---|
89 |
perm=[dim:adims 1:dim-1]; % permutation vector |
---|
90 |
a=permute(a,perm); % make DIM first dimension |
---|
91 |
a=reshape(a,N,[]); % make A 2D |
---|
92 |
ac=cell(N,1); |
---|
93 |
for k=1:N % places rows of A into cells |
---|
94 |
ac{k}=a(k,:); |
---|
95 |
end |
---|
96 |
for k=1:lind % determine size and class of inputs |
---|
97 |
if ~isequal(class(c{k}),aclass) |
---|
98 |
error('interlace:ClassConflict','B or C Must Have Same Class as A') |
---|
99 |
end |
---|
100 |
sc=[size(c{k}) ones(1,adims-ndims(c{k}))]; |
---|
101 |
if all(sc==1) % scalar expansion |
---|
102 |
c{k}=repmat(c{k},asiz); |
---|
103 |
elseif any(sc(notdim)~=asiz(notdim)) % check conformity |
---|
104 |
error('interlace:ArgumentMisMatch',... |
---|
105 |
'Contents of B or C Incorrect Size for A.') |
---|
106 |
end |
---|
107 |
tmp=permute(c{k},perm); |
---|
108 |
c{k}=reshape(tmp,size(tmp,1),[]); % reshape to 2D like ac |
---|
109 |
end |
---|
110 |
yc=[ac;c]; % stack cells |
---|
111 |
[idx,idx]=sort([n;ind]); % sort indices of A and IND |
---|
112 |
yc=yc(idx); % apply sort to A and C |
---|
113 |
y=cat(1,yc{:}); % convert back to array from cell |
---|
114 |
asiz(dim)=size(y,1); % new size along DIM |
---|
115 |
y=reshape(y,asiz(perm)); % put result back in original form |
---|
116 |
y=ipermute(y,perm); % inverse permute dimensions |
---|