Addition of time series' values in array

3 次查看(过去 30 天)
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.

回答(2 个)

Laurens Bakker
Laurens Bakker 2012-3-7
Why not just use a for loop?
sum = zeros( size(OBJECTS(1).PROP.Data );
for obj=OBJECTS
sum = sum + obj.PROP.Data;
end
  1 个评论
David
David 2012-3-13
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.

请先登录,再进行评论。


owr
owr 2012-3-13
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

类别

Help CenterFile Exchange 中查找有关 Descriptive Statistics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by