function m1 = placematrix2(m1,m2,pos) <m1> is a matrix (cell matrix okay) <m2> is a matrix (cell matrix okay) <pos> (optional) is [x1 x2 x3 ...] with a position. x1, x2, x3, etc. must be positive integers, and there must be at least ndims(m2) of these numbers. default is ones(1,ndims(m2)). place <m2> in <m1> positioned with first element at <pos>. <m1> and <m2> must both be of the same type (matrix or cell matrix). if <m1> is not large enough to accommodate <m2>, 0s or []s are automatically filled in. example: isequal(placematrix2([1 2 3; 4 5 6; 7 8 9],[0 0],[2 1]),[1 2 3; 0 0 6; 7 8 9])
0001 function m1 = placematrix2(m1,m2,pos) 0002 0003 % function m1 = placematrix2(m1,m2,pos) 0004 % 0005 % <m1> is a matrix (cell matrix okay) 0006 % <m2> is a matrix (cell matrix okay) 0007 % <pos> (optional) is [x1 x2 x3 ...] with a position. 0008 % x1, x2, x3, etc. must be positive integers, and 0009 % there must be at least ndims(m2) of these numbers. 0010 % default is ones(1,ndims(m2)). 0011 % 0012 % place <m2> in <m1> positioned with first element at <pos>. 0013 % <m1> and <m2> must both be of the same type (matrix or cell matrix). 0014 % if <m1> is not large enough to accommodate <m2>, 0s or []s 0015 % are automatically filled in. 0016 % 0017 % example: 0018 % isequal(placematrix2([1 2 3; 4 5 6; 7 8 9],[0 0],[2 1]),[1 2 3; 0 0 6; 7 8 9]) 0019 0020 %% SEE ALSO padarray.m ? 0021 0022 % input 0023 if ~exist('pos','var') || isempty(pos) 0024 pos = ones(1,ndims(m2)); 0025 end 0026 0027 % figure out indices 0028 indices = {}; 0029 for p=1:length(pos) 0030 indices{p} = pos(p)-1 + (1:size(m2,p)); 0031 end 0032 0033 % do it 0034 m1(indices{:}) = m2;