1 |
function [C,RA,RB] = insertrows(A,B,ind) |
---|
2 |
% INSERTROWS - Insert rows into a matrix at specific locations |
---|
3 |
% C = INSERTROWS(A,B,IND) inserts the rows of matrix B into the matrix A at |
---|
4 |
% the positions IND. Row k of matrix B will be inserted after position IND(k) |
---|
5 |
% in the matrix A. If A is a N-by-X matrix and B is a M-by-X matrix, C will |
---|
6 |
% be a (N+M)-by-X matrix. IND can contain non-integers. |
---|
7 |
% |
---|
8 |
% If B is a 1-by-N matrix, B will be inserted for each insertion position |
---|
9 |
% specified by IND. If IND is a single value, the whole matrix B will be |
---|
10 |
% inserted at that position. If B is a single value, B is expanded to a row |
---|
11 |
% vector. In all other cases, the number of elements in IND should be equal to |
---|
12 |
% the number of rows in B, and the number of columns, planes etc should be the |
---|
13 |
% same for both matrices A and B. |
---|
14 |
% If any of the inputs are empty, C will return A. |
---|
15 |
% |
---|
16 |
% Examples: |
---|
17 |
% % the size of A,B, and IND all match |
---|
18 |
% C = insertrows(rand(5,2),zeros(2,2),[1.5 3]) |
---|
19 |
% % the row vector B is inserted twice |
---|
20 |
% C = insertrows(ones(4,3),1:3,[1 Inf]) |
---|
21 |
% % matrix B is expanded to a row vector and inserted twice (as in 2) |
---|
22 |
% C = insertrows(ones(5,3),999,[2 4]) |
---|
23 |
% % the whole matrix B is inserted once |
---|
24 |
% C = insertrows(ones(5,3),zeros(2,3),2) |
---|
25 |
% |
---|
26 |
% [C, RA, RB] = INSERTROWS(...) will return the row indices RA and RB for which C |
---|
27 |
% corresponds to the rows of either A and B. |
---|
28 |
% [c,ra,rb] = insertrows([1:4].',99,[0 3]) ; |
---|
29 |
% c.' % -> [99 1 2 3 99 4] ; |
---|
30 |
% c(ra).' % -> [1 2 3 4] ; |
---|
31 |
% c(rb).' % -> [99 99] ; |
---|
32 |
% |
---|
33 |
% To insert columns, planes, etc., you can permute the inputs and ipermute the result. |
---|
34 |
% A = ones(4,3) ; B = zeros(4,1) ; |
---|
35 |
% C = insertrows(permute(A,[2 1]), permute(B,[2 1]),1) ; |
---|
36 |
% C = ipermute(C,[2 1]) ; |
---|
37 |
% |
---|
38 |
% See also PERMUTE, IPERMUTE, RESHAPE, CAT |
---|
39 |
|
---|
40 |
% for Matlab R13 |
---|
41 |
% version 1.0 (feb 2006) |
---|
42 |
% (c) Jos van der Geest |
---|
43 |
% email: jos@jasen.nl |
---|
44 |
|
---|
45 |
error(nargchk(3,3,nargin)) ; |
---|
46 |
|
---|
47 |
% shortcut when any of the inputs are empty |
---|
48 |
if isempty(B) || isempty(ind), |
---|
49 |
C = A ; |
---|
50 |
if nargout, |
---|
51 |
RA = 1:size(A,1) ; |
---|
52 |
RB = [] ; |
---|
53 |
end |
---|
54 |
end |
---|
55 |
|
---|
56 |
sa = size(A) ; |
---|
57 |
|
---|
58 |
% match the sizes of A, B |
---|
59 |
if numel(B)==1, |
---|
60 |
% B has a single argument, expand to match A |
---|
61 |
sb = [1 sa(2:end)] ; |
---|
62 |
B = repmat(B,sb) ; |
---|
63 |
else |
---|
64 |
% otherwise check for dimension errors |
---|
65 |
if ndims(A) ~= ndims(B), |
---|
66 |
error('Both input matrices should have the same number of dimensions.') ; |
---|
67 |
end |
---|
68 |
sb = size(B) ; |
---|
69 |
if ~all(sa(2:end) == sb(2:end)), |
---|
70 |
error('Both input matrices should have the same number of columns (and planes, etc).') ; |
---|
71 |
end |
---|
72 |
end |
---|
73 |
|
---|
74 |
ind = ind(:) ; % make as row vector |
---|
75 |
ni = length(ind) ; |
---|
76 |
|
---|
77 |
% match the sizes of B and IND |
---|
78 |
if ni ~= sb(1), |
---|
79 |
if ni==1 && sb(1) > 1, |
---|
80 |
% expand IND |
---|
81 |
ind = repmat(ind,sb(1),1) ; |
---|
82 |
elseif (ni > 1) && (sb(1)==1), |
---|
83 |
% expand B |
---|
84 |
B = repmat(B,ni,1) ; |
---|
85 |
else |
---|
86 |
error('The number of rows to insert should equal the number of insertion positions.') ; |
---|
87 |
end |
---|
88 |
end |
---|
89 |
sb = size(B) ; |
---|
90 |
|
---|
91 |
% the actual work |
---|
92 |
% 1. concatenate matrices |
---|
93 |
C = [A ; B] ; |
---|
94 |
% 2. sort the respective indices |
---|
95 |
[abi,abi] = sort([[1:sa(1)].' ; ind(:)]) ; |
---|
96 |
% 3. reshuffle the large matrix |
---|
97 |
C = C(abi,:) ; |
---|
98 |
% 4. reshape as A for nd matrices (nd>2) |
---|
99 |
if ndims(A) > 2, |
---|
100 |
sc = sa ; |
---|
101 |
sc(1) = sc(1)+sb(1) ; |
---|
102 |
C = reshape(C,sc) ; |
---|
103 |
end |
---|
104 |
|
---|
105 |
if nargout, |
---|
106 |
% outputs required |
---|
107 |
R = [zeros(sa(1),1) ; ones(sb(1),1)] ; |
---|
108 |
R = R(abi) ; |
---|
109 |
RA = find(R==0) ; |
---|
110 |
RB = find(R==1) ; |
---|
111 |
end |
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 |
|
---|
117 |
|
---|