257 views (last 30 days)

Show older comments

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.

Jan
on 26 Mar 2018

Edited: Jan
on 28 Mar 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

Jan
on 28 Mar 2018

Jan
on 28 Mar 2018

Edited: Jan
on 28 Mar 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.

Bruno Luong
on 28 Mar 2018

Edited: Bruno Luong
on 28 Mar 2018

sf2 = arrayfun(@(s) s.field(2), s)

arnold
on 2 Jan 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.

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

Start Hunting!