Addition of time series' values in array

19 views (last 30 days)
David
David on 6 Mar 2012
Hello,
I have a tricky one I haven't been able to solve. I have an array of objects of a specific class. One of the properties of this class is a time series. I can guarantee that all the time series for that particular property for all objects in the array have exactly the same time vector. I would like to sum all the values of that property (i.e. of those time series) into a single time series with the same time vector.
For example, I have an array OBJECTS composed of objects of class OBJ with property PROP of type timeseries. I want to add all the data values of the PROP time series.
I tried the following:
sum(OBJECTS(:).PROP.Data)
but didn't work. I'm not sure about the OBJECTS(:) part, in particular the (:). I made several different tests, also playing with the dimension for sum (first or second) but without success.
Any ideas? Thanks in advance for any suggestion.
  1 Comment
per isakson
per isakson on 6 Mar 2012
What happend? Did you get an error message?

Sign in to comment.

Answers (2)

Laurens Bakker
Laurens Bakker on 7 Mar 2012
Why not just use a for loop?
sum = zeros( size(OBJECTS(1).PROP.Data );
for obj=OBJECTS
sum = sum + obj.PROP.Data;
end
  1 Comment
David
David on 13 Mar 2012
I am currently using a loop as a work-around, but I was hoping to use SUM as it should be much faster. But thanks for the suggestion.

Sign in to comment.


owr
owr on 13 Mar 2012
The double level of "." indexing could cause an issue, but if it was just single level this trick might work:
>> foo(1).data = rand(5,1);
>> foo(2).data = rand(5,1);
>> foo(:).data
ans =
0.7577
0.7431
0.3922
0.6555
0.1712
ans =
0.7060
0.0318
0.2769
0.0462
0.0971
>> [foo(:).data]
ans =
0.7577 0.7060
0.7431 0.0318
0.3922 0.2769
0.6555 0.0462
0.1712 0.0971
>> sum([foo(:).data],2)
ans =
1.4638
0.7750
0.6692
0.7016
0.2683

Community Treasure Hunt

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

Start Hunting!

Translated by