# Vectorizing Access to an Array of Structures 40

Posted by **Loren Shure**,

Though I have written about this topic before, I continue to get questions on working with arrays of structures. So I thought I would focus on that alone today.

### Contents

### Recent Sample Question

Recently there was a question on the newsgroup about how to vectorize the access to elements in a structure array. And also got an email from a customer on this topic. Here is one customer's description of his problem:

In a nutshell, what I am trying to do (in as few lines of code as possible) is:

state = an array of structs, say N items each with (for example) components x and y.

In my case 'state' is reasonably complicated and hence does not warrant the use of a simple 2 x N matrix.

for i=2:N state(i)=markovmodel(state(i-1)); % 1. Access individual structs end

plot(state.x) % 2. Access all of the entries of one element as vector.

### The Answer

Let's create an array of structures first.

states(1).x = 3; states(2).x = 4; states(3).x = 5; states(1).y = 42; states(2).y = 99; states(3).y = 0; states

states = 1x3 struct array with fields: x y

Let's see what's in states.x

states.x

ans = 3 ans = 4 ans = 5

With an array of structs, you can gather the `x` values using

allxvals = [states.x]

allxvals = 3 4 5

This is because `states.x` produces a comma-separated list. How do I know this? Because, if I leave the semi-colon off, I see a bunch of output with `ans =`; in other words, I am getting multiple output values. How do I collect multiple values into an array? I can use either
square brackets or curly braces.

Using the square brackets, `[]`, just places that list into between the brackets, creating an array of one type, in this case, doubles. You can do likewise
with struct arrays with fields that are more suitable for a cell array using curly braces, `{}` in place of the square brackets.

### User Solution

If instead of using

plot(state.x) above, the user replaced this with

plot([state.x])

the informationg gets plotted correctly.

### Comments?

My question to you is why this question keeps getting asked. Is the concept unusual enough that people don't know how to even look for the information on-line? What can we do at The MathWorks to make this more visible? Please pass along your thoughts here.

Get the MATLAB code

Published with MATLAB® 7.4

**Category:**- Structures,
- Vectorization

### Note

Comments are closed.

## 40 CommentsOldest to Newest

**1**of 40

**2**of 40

**3**of 40

**4**of 40

**5**of 40

**6**of 40

>> a(1).x = 10; a(2).x = 25; a(1).y.m = 1000; a(2).y.m = 3500; a(2).y.n = 45; a(1).y.n = 1; >> a a = 1x2 struct array with fields: x y >> a.y ans = m: 1000 n: 1 ans = m: 3500 n: 45 >> a(3).y.q = 17 a = 1x3 struct array with fields: x y >> a(1).y ans = m: 1000 n: 1 >> a(3).y ans = q: 17Now if I try to gather things together for, let's say a.y, I don't even have consistent contents for them. Different fields, etc. So MATLAB can't know a priori that the operation you want to do will work. My own belief is that we might solve this by adding something more structured to MATLAB, something like a "uniform" structure, and when the user choses this, s/he guarantees that the fields all exist the same throughout their array. Then we can nest the access and gathering process. --Loren

**7**of 40

**8**of 40

**9**of 40

**10**of 40

**11**of 40

**12**of 40

**13**of 40

**14**of 40

**15**of 40

**16**of 40

**17**of 40

**18**of 40

**19**of 40

**20**of 40

**21**of 40

**22**of 40

**23**of 40

**24**of 40

**25**of 40

**26**of 40

**27**of 40

**28**of 40

tic Distance= ForLoop(A); loopTime = toc/XYTimeand this subfunction to the file:

function d= ForLoop(A) sz=numel(A); d=zeros(sz,1); for ind=1:sz d(ind)=A(sz).x-A(sz).y; endPersonally, I find it less than ideal that none of the vectorized solutions behave as well. I have reported this to the development team. --Loren

**29**of 40

```
N = 1e5;
x = rand(1, N);
y = rand(1, N);
A1 = struct('x', num2cell(x), 'y', num2cell(y));
A2 = struct('x', x, 'y', y);
whos A1 A2
tic; d1 = [A1.x] + [A1.y]; toc,
tic; d2 = A2.x + A2.y; toc,
isequal(d1, d2)
```

If you want to interact with each point individually more often, then the array of structs would probably be the right approach.
`point1 = A1(1);`

Getting the point1 matrix from A2 would be messier. You could do it with the FIELDNAMES function, but I don't think it would be a nice one-liner.
If instead you want to interact with the X coordinate of all the points more often, the struct containing an array would be better suited for your application.
`allX = A2.x;`

Getting the allX matrix from A1 isn't messier (like point1 from A2 above) but it would be slower as you posted.
So which data layout you should use to get better performance depends somewhat on what tasks you want to carry out most often. **30**of 40

**31**of 40

**32**of 40

**33**of 40

**34**of 40

**35**of 40

**36**of 40

**37**of 40

find(list_uwv.cord(:) == [0 3.5 0.8]))Up to now I've placed the vectors in a matrix (array of Nx3 elements) and I make the query to the matrix by using find function. TNX a lot Loren

**38**of 40

**39**of 40

**40**of 40

c(3).x = [] [c.y] = c.x d = 1:3 dc = num2cell(d) [c.x] = dc{:} c.x--Loren

## Recent Comments