VARSETS_IDX Returns a vector of indices into X specified by VARSETS K = OM.VARSETS_IDX(VARSETS) Returns a vector of indices into X corresponding to the variable sets specified by VARSETS. See also VARSET_X
0001 function kk = varsets_idx(om, vs) 0002 %VARSETS_IDX Returns a vector of indices into X specified by VARSETS 0003 % K = OM.VARSETS_IDX(VARSETS) 0004 % 0005 % Returns a vector of indices into X corresponding to the variable 0006 % sets specified by VARSETS. 0007 % 0008 % See also VARSET_X 0009 0010 % MP-Opt-Model 0011 % Copyright (c) 2017-2020, Power Systems Engineering Research Center (PSERC) 0012 % by Ray Zimmerman, PSERC Cornell 0013 % 0014 % This file is part of MP-Opt-Model. 0015 % Covered by the 3-clause BSD License (see LICENSE file for details). 0016 % See https://github.com/MATPOWER/mp-opt-model for more info. 0017 0018 persistent sn; 0019 if isempty(vs) 0020 kk = (1:om.var.N); 0021 else 0022 vsN = length(vs); 0023 k = cell(1, vsN); %% indices for varsets 0024 0025 %% calls to substruct() are relatively expensive, so we pre-build the 0026 %% struct for addressing numeric array fields, updating only 0027 %% the subscripts before use 0028 if isempty(sn) 0029 sn = struct('type', {'.', '()'}, 'subs', {'', 1}); 0030 end 0031 0032 ki = 0; 0033 for v = 1:vsN 0034 vname = vs(v).name; 0035 vidx = vs(v).idx; 0036 if isempty(vidx) 0037 i1 = om.var.idx.i1.(vname); %% starting index in full x 0038 iN = om.var.idx.iN.(vname); %% ending index in full x 0039 else 0040 % (calls to substruct() are relatively expensive ... 0041 % sn = substruct('.', vname, '()', vidx); 0042 % ... so replace it with these more efficient lines) 0043 sn(1).subs = vname; 0044 sn(2).subs = vidx; 0045 i1 = subsref(om.var.idx.i1, sn); %% starting index in full x 0046 iN = subsref(om.var.idx.iN, sn); %% ending index in full x 0047 end 0048 if isscalar(i1) %% simple named set, or indexed named set 0049 ki = ki + 1; 0050 k{ki} = (i1:iN); %% single set of indices for varset 0051 else %% multi-dim named set w/no index specified 0052 ii1 = permute(i1, ndims(i1):-1:1); 0053 iiN = permute(iN, ndims(i1):-1:1); 0054 for j = 1:length(ii1(:)) 0055 ki = ki + 1; 0056 k{ki} = (ii1(j):iiN(j)); %% multiple sets of indices for varset 0057 end 0058 end 0059 end 0060 kk = [k{:}]; 0061 end