For a multidimensional array is it possible to return all other array dimensions for a single index of one dimension without having to use the colon for indexing?

5 vues (au cours des 30 derniers jours)
I am working on a problem where an input space of n inputs is being partitioned. I have a structure containing all of the models (which have been partitioned), which has n-dimensions where the number of models in each dimension is the number of partitions in that dimension.
I want to create a copy of a set of models which correspond to a particular index in one of the n dimensions, advance all of the model sets beyond that index to their original index + 1, then insert the copy next to the original.
The problem can be solved by using eval (see code below), but it is a bit messy and I wondered if there is a more succinct way without having to generate text strings containing loads of colons and commas?
Mdls = struct(); % Initialise Mdls as struct
Mdls = repmat(Mdls,[2,4,6,8,6,4,2]); % Arbitrarily size the struct
dim = 3; % dimension to elongate
ind2c = 3; % Index in dimension "dim" to copy
eval(['MdlsNew = ',repmat(['cat(',num2str(dim),','],1,2),...
'Mdls(',strjoin(repmat({':,'},1,dim-1)),'1:ind2c',strjoin(repmat({',:'},1,length(size(Mdls))-dim)),'),',...
'Mdls(',strjoin(repmat({':,'},1,dim-1)),'ind2c',strjoin(repmat({',:'},1,length(size(Mdls))-dim)),')),',...
'Mdls(',strjoin(repmat({':,'},1,dim-1)),'(ind2c+1):end',strjoin(repmat({',:'},1,length(size(Mdls))-dim)),'));'])

Réponse acceptée

Bruno Luong
Bruno Luong le 11 Juil 2019
Mdls = struct(); % Initialise Mdls as struct
Mdls = repmat(Mdls,[2,4,6,8,6,4,2]); % Arbitrarily size the struct
dim = 3; % dimension to elongate
ind2c = 3; % Index in dimension "dim" to copy
s = size(Mdls);
c = cell(size(s));
c(:) = {':'};
c{dim} = [1:ind2c ind2c:s(dim)];
MdlsNew =Mdls(c{:})

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by