How do you index an array inside an array of structures?
27 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Neil Patel
el 26 de Mzo. de 2018
Respondida: arnold
el 2 de En. de 2019
I would like to index an array which is one of the fields of an array of structures. For example if I have something like:
s(1).field = [1 2 3 4];
s(2).field = [2 4 6 8];
I would like to access all of the 2nd values of s.field. However something like [s.field(2)] gives the error "Expected one output from a curly brace or dot indexing expression, but there were 2 results." I know there are other ways to structure this data but I'm just constructing the simplest example possible (my real application has other fields which are not arrays and a much larger number of entries). With a structure in this format I know I could call [s.field] and reshape it and index but that's a bit hard to read. My current solution is to loop through all the entries but I feel like there must be a better solution.
0 comentarios
Respuesta aceptada
Jan
el 26 de Mzo. de 2018
Editada: Jan
el 28 de Mzo. de 2018
There is no direct method for "nested indexing". Your loop method is the best way, if you pre-allocate the output.
This is a disadvantage of the chosen structure. Using a different method to store the values might be more convenient. Sometimes this can be seen, when the program is in its final state when the bottlenecks are optimized. Therefore it is a smart strategy to write the code as independent as possible from the underlying structure of the data, such that it can be adjusted later without the need to rewrite the complete code.
len = size(s, 1);
v = zeros(len, 1);
for kk = 1:len
v(kk) = s(kk).field(2);
end
4 comentarios
Jan
el 28 de Mzo. de 2018
You can create a C-Mex function, which can extract the values faster than the loop in Matlab - probably. But it cannot be very flexible. If the struct array has multiple dimensions and you do not want to extract a scalar, but a vector from the fields, the output might be confusing in general.
Más respuestas (4)
Jan
el 28 de Mzo. de 2018
Editada: Jan
el 28 de Mzo. de 2018
Some timings:
for k = 5000:-1:1 % Backwards for implicit allocation
s(k, 1).field = rand(1, 4);
end
tic;
for k = 1:50
v1 = arrayfun(@(s) s.field(2), s);
end
toc
tic;
for k = 1:50
tmp = vertcat(s.field);
v2 = tmp(:,2);
end
toc
tic;
for k = 1:50
len = size(s, 1);
v3 = zeros(len, 1);
for kk = 1:len
v3(kk) = s(kk).field(2);
end
end
toc
Matlab R2016b/Win7:
Elapsed time is 5.150233 seconds. arrayfun
Elapsed time is 0.155297 seconds. vertcat
Elapsed time is 0.237163 seconds. loop
Stephen's solution is the fastest, but even a loop outperforms arrayfun by a factor of 20. But if the data are larger, creating the temporary array gets more expensive: For s(k).field = rand(1, 400) the timings are:
Elapsed time is 4.973404 seconds. arrayfun
Elapsed time is 1.911395 seconds. vertcat
Elapsed time is 0.257397 seconds. loop
By the way: Do you see the variation for arrayfun? Obviously tic/toc is not very precise. timeit would be better, but here the coarse values are sufficient to show the magnitude of the effects.
The best solution is to decide for storing the data in a numerical array directly instead of distribute it to a struct array. This has another advantage: Each variable, element in a cell and field in a struct needs about 100 bytes overhead. Therefore using one numerical array to store the data can save a lot of time for creating the data and memory.
2 comentarios
Bruno Luong
el 28 de Mzo. de 2018
The vertcat solution runtime/memory requirement depends also on the length of the field (and assume data have the same length).
For-loop is a safe bet.
We have discussed some time to time about performance limitation of ARRAYFUN and CELLFUN, the only advantage is compact code.
Bruno Luong
el 28 de Mzo. de 2018
Editada: Bruno Luong
el 28 de Mzo. de 2018
sf2 = arrayfun(@(s) s.field(2), s)
0 comentarios
arnold
el 2 de En. de 2019
I've been looking for the same thing and have grown pretty frustrated with this having to create temporary variables or loops all over the place for a long time. Thank you @Jan to have clarified that there is no better option.
I'd love somebody's input on these: I want the second component of the Centroid of the structure given by the regionprops function.
My wishful thinking would be this working
idx = [1,10,55,832];
XCentroids = vertcat(RegionPropsOut(idx).Centroid(2));
Expected one output from a curly brace or dot indexing expression,
but there were 4 results.
with
size(vertcat(RegionPropsOut.Centroid))
ans =
16421 2
but yeah, this doesn't work. So I thought for years there might be a some notation like
XCentroids = vertcat(RegionPropsOut(idx).Centroid)(:,2);
but sure, there is nothing like the "appended parenthesis idea".
There is also no other one-liner that might do it, right.... this would be another idea but no
[~, XCentroid] = vertcat(RegionPropsOut(idx).Centroid);
which gives
Error using vertcat
Too many output arguments.
Is there really no nicer way? Instead of looping I still prefer the also ugly temporary variable like such...
tmp = vertcat(stats1.Centroid);
XCentroid = tmp(:,2);
clear tmp;
this is one of the annoyances I encounter far too often. I'd appreciate an idea to make this nicer and less memory intensive.
0 comentarios
Ver también
Categorías
Más información sobre Structures en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!