0
votes

I have a struct call CDF of 3 fields with are all double arrays, size 1x48. (below) enter image description here

I need to get the average (or mean) of the cdfSR field but across each struct element. If I'm not being clear enough I need

[ sum(CDF(:).cdfSR(1))/895, sum(CDF(:).cdfSR(2))/895, ..., sum(CDF(:).cdfSR(48))/895 ]

Each time I try to implement "CDF(:).cdfSR(1)", I receive an error:

Expected one output from a curly brace or dot indexing expression, but there were 895 results.

However, I want all 895 results.

1

1 Answers

2
votes

The expression

CDF(:).cdfSR(1)

returns a comma-separated list with each of the ii=1:895 elements CDF(ii).cdfSR(1). You can capture these using square brackets:

[CDF(:).cdfSR(1)]

is equivalent to

[CDF(1).cdfSR(1), CDF(2).cdfSR(1), CDF(3).cdfSR(1), ...]

So the code you posted can be written as:

[ sum([CDF(:).cdfSR(1)])/895, sum([CDF(:).cdfSR(2)])/895, ..., sum([CDF(:).cdfSR(48)])/895 ]

But of course this is not viable either. Since CDF(ii).cdfSR is a horizontal vector, I suggest you concatenate them vertically:

vertcat(CDF(:).cdfSR)

vertcat(a,b,c) is the same as [a;b;c]. You can also use cat(1,...).

This leads to a 895x48 double array. You can take the mean using mean:

mean(vertcat(CDF(:).cdfSR), 1);