SET_REORDER Assigns B to A with one of the dimensions of A indexed. A = SET_REORDER(A, B, IDX, DIM) Returns A after doing A(:, ..., :, IDX, :, ..., :) = B where DIM determines in which dimension to place the IDX. If any dimension of B is smaller than the corresponding dimension of A, the "extra" elements in A are untouched. If any dimension of B is larger than the corresponding dimension of A, then A is padded with zeros (if numeric) or empty matrices (if cell array) before performing the assignment. See also GET_REORDER.
0001 function A = set_reorder(A, B, idx, dim) 0002 %SET_REORDER Assigns B to A with one of the dimensions of A indexed. 0003 % 0004 % A = SET_REORDER(A, B, IDX, DIM) 0005 % 0006 % Returns A after doing A(:, ..., :, IDX, :, ..., :) = B 0007 % where DIM determines in which dimension to place the IDX. 0008 % 0009 % If any dimension of B is smaller than the corresponding dimension 0010 % of A, the "extra" elements in A are untouched. If any dimension of 0011 % B is larger than the corresponding dimension of A, then A is padded 0012 % with zeros (if numeric) or empty matrices (if cell array) before 0013 % performing the assignment. 0014 % 0015 % See also GET_REORDER. 0016 0017 % MATPOWER 0018 % Copyright (c) 2009-2016, Power Systems Engineering Research Center (PSERC) 0019 % by Ray Zimmerman, PSERC Cornell 0020 % 0021 % This file is part of MATPOWER. 0022 % Covered by the 3-clause BSD License (see LICENSE file for details). 0023 % See http://www.pserc.cornell.edu/matpower/ for more info. 0024 0025 %% check dimensions 0026 ndim = ndims(A); 0027 sA = size(A); 0028 sB = size(B); 0029 d = (1:length(sA)); 0030 d(dim) = []; %% indices of all dimensions other than DIM 0031 0032 %% pad A with zeros (numeric) or empty matrices (cell), if necessary 0033 s.subs = cell(1, ndim); 0034 if any(sA(d) < sB(d)) 0035 s.subs = num2cell(max(sA, sB)); 0036 if iscell(A) 0037 s.type = '{}'; 0038 A = subsasgn(A, s, []); 0039 else 0040 s.type = '()'; 0041 A = subsasgn(A, s, 0); 0042 end 0043 end 0044 0045 %% set up indexing 0046 s.type = '()'; 0047 for k = 1:ndim 0048 if k == dim 0049 s.subs{k} = idx; 0050 else 0051 if sA(k) == sB(k) 0052 s.subs{k} = ':'; %% indexes of all elements in this dimension 0053 else %% sA(k) > sB(k) 0054 s.subs{k} = (1:sB(k)); %% limit indexes to smaller size of B 0055 end 0056 end 0057 end 0058 0059 %% do the assignment 0060 A = subsasgn(A, s, B);