Addition of time series' values in array

3 ビュー (過去 30 日間)
David
David 2012 年 3 月 6 日
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 件のコメント
per isakson
per isakson 2012 年 3 月 6 日
What happend? Did you get an error message?

サインインしてコメントする。

回答 (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 Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by