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 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