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-2015 by Power System Engineering Research Center (PSERC) 0019 % by Ray Zimmerman, PSERC Cornell 0020 % 0021 % $Id: set_reorder.m 2644 2015-03-11 19:34:22Z ray $ 0022 % 0023 % This file is part of MATPOWER. 0024 % Covered by the 3-clause BSD License (see LICENSE file for details). 0025 % See http://www.pserc.cornell.edu/matpower/ for more info. 0026 0027 %% check dimensions 0028 ndim = ndims(A); 0029 sA = size(A); 0030 sB = size(B); 0031 d = (1:length(sA)); 0032 d(dim) = []; %% indices of all dimensions other than DIM 0033 0034 %% pad A with zeros (numeric) or empty matrices (cell), if necessary 0035 s.subs = cell(1, ndim); 0036 if any(sA(d) < sB(d)) 0037 s.subs = num2cell(max(sA, sB)); 0038 if iscell(A) 0039 s.type = '{}'; 0040 A = subsasgn(A, s, []); 0041 else 0042 s.type = '()'; 0043 A = subsasgn(A, s, 0); 0044 end 0045 end 0046 0047 %% set up indexing 0048 s.type = '()'; 0049 for k = 1:ndim 0050 if k == dim 0051 s.subs{k} = idx; 0052 else 0053 if sA(k) == sB(k) 0054 s.subs{k} = ':'; %% indexes of all elements in this dimension 0055 else %% sA(k) > sB(k) 0056 s.subs{k} = (1:sB(k)); %% limit indexes to smaller size of B 0057 end 0058 end 0059 end 0060 0061 %% do the assignment 0062 A = subsasgn(A, s, B);