How do I use indexing (vectorization?) rather than for loops for dynamic field referencing into a cell array?
Afficher commentaires plus anciens
I think there's a way to eliminate for loops by indexing, or perhaps it's called vectorizing. How do I do this?
For example, I'm trying to plot several vectors of different lengths in the same 3D scatter plot. This code stores 34 DICOM ROI items' double arrays in a 1x34 cell array:
info = dicominfo('filename.dcm');
numberofarrays = 34;
datacells = cell(1,numberofarrays);
names = fieldnames(info.ROIContourSequence.Item_9.ContourSequence);
for loop = 1:numberofarrays
datacells(loop) = {info.ROIContourSequence.Item_9.ContourSequence.(names{loop}).ContourData(:,1)};
end
but this code results in the following error:
datacellsfaster(1:34) = {info.ROIContourSequence.Item_9.ContourSequence.(names{1:34}).ContourData(:,1)};
Expected one output from a curly brace or dot indexing expression, but there were 34 results.
Why was it expecting only one output? How do I eliminate for loops through indexing?
Réponse acceptée
Plus de réponses (1)
geotocho
le 28 Oct 2017
Rather than curly brace indexing, the vectorized way of assigning a command to all cells uses parentheses. Much like the traditional array but in this example A and B are cell arrays of the same size. I wish to assign the row of cells in A to B. In this manner dot indexing is allowed.
B(1,:) = A(1,:);
1 commentaire
Daniel Bridges
le 20 Nov 2017
Catégories
En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!