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 % $Id: set_reorder.m 2423 2014-11-13 16:43:10Z ray $ 0019 % by Ray Zimmerman, PSERC Cornell 0020 % Copyright (c) 2009-2010 by Power System Engineering Research Center (PSERC) 0021 % 0022 % This file is part of MATPOWER. 0023 % See http://www.pserc.cornell.edu/matpower/ for more info. 0024 % 0025 % MATPOWER is free software: you can redistribute it and/or modify 0026 % it under the terms of the GNU General Public License as published 0027 % by the Free Software Foundation, either version 3 of the License, 0028 % or (at your option) any later version. 0029 % 0030 % MATPOWER is distributed in the hope that it will be useful, 0031 % but WITHOUT ANY WARRANTY; without even the implied warranty of 0032 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0033 % GNU General Public License for more details. 0034 % 0035 % You should have received a copy of the GNU General Public License 0036 % along with MATPOWER. If not, see <http://www.gnu.org/licenses/>. 0037 % 0038 % Additional permission under GNU GPL version 3 section 7 0039 % 0040 % If you modify MATPOWER, or any covered work, to interface with 0041 % other modules (such as MATLAB code and MEX-files) available in a 0042 % MATLAB(R) or comparable environment containing parts covered 0043 % under other licensing terms, the licensors of MATPOWER grant 0044 % you additional permission to convey the resulting work. 0045 0046 %% check dimensions 0047 ndim = ndims(A); 0048 sA = size(A); 0049 sB = size(B); 0050 d = (1:length(sA)); 0051 d(dim) = []; %% indices of all dimensions other than DIM 0052 0053 %% pad A with zeros (numeric) or empty matrices (cell), if necessary 0054 s.subs = cell(1, ndim); 0055 if any(sA(d) < sB(d)) 0056 s.subs = num2cell(max(sA, sB)); 0057 if iscell(A) 0058 s.type = '{}'; 0059 A = subsasgn(A, s, []); 0060 else 0061 s.type = '()'; 0062 A = subsasgn(A, s, 0); 0063 end 0064 end 0065 0066 %% set up indexing 0067 s.type = '()'; 0068 for k = 1:ndim 0069 if k == dim 0070 s.subs{k} = idx; 0071 else 0072 if sA(k) == sB(k) 0073 s.subs{k} = ':'; %% indexes of all elements in this dimension 0074 else %% sA(k) > sB(k) 0075 s.subs{k} = (1:sB(k)); %% limit indexes to smaller size of B 0076 end 0077 end 0078 end 0079 0080 %% do the assignment 0081 A = subsasgn(A, s, B);