sum of two different data series

조회 수: 2 (최근 30일)
Hamad
Hamad 2014년 8월 6일
댓글: Hamad 2014년 8월 6일
Hey all. I would like some help please on finding an efficient way of doing this:
I have two data series combined into one vector, so something like the below example
data =
DataPoint DataIdx
0.50 1
0.25 1
0.80 2
0.10 1
0.60 1
0.50 2
0.25 2
0.50 2
0.40 1
0.10 1
0.50 1
What I would like to do is add up all the data points per dataIdx, but preserve the order. So the output would be a "consolidated" vector which would look something like:
output =
DataConsolidated DataIdx
0.75 1
0.80 2
0.70 1
1.25 2
1.00 1
At the moment, I am using a for-loop that keeps a tally, but I hope there is a more elegant solution. I think this might lend itself well to an application of "accumarray", but I have not that much experience with this function. Any help pls?
Thanks! Hamad
  댓글 수: 1
Image Analyst
Image Analyst 2014년 8월 6일
Maybe it's better, for code maintainability and ease of understanding it (especially later), if you just go with a simple intuitive for loop rather than some cryptic accumarray function that you don't understand. Simplicity has its benefits.

댓글을 달려면 로그인하십시오.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 8월 6일
A=[0.50 1
0.25 1
0.80 2
0.10 1
0.60 1
0.50 2
0.25 2
0.50 2
0.40 1
0.10 1
0.50 1 ]
jj=[0; diff(A(:,2))~=0];
kk=cumsum(jj);
[~,id]=unique(kk);
out=[ accumarray(kk+1,A(:,1)) A(id,2)]
  댓글 수: 1
Hamad
Hamad 2014년 8월 6일
Efficient, concise, and informative. Thanks very much! Hamad.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2014년 8월 6일
You cannot use DataIdx directly as subs in accumarray() because in your example each streak of indices is treated as a separate accumulation block while the labeling is repeated, i.e. the 1s appear again after 2. You need to re-label the DataIdx and accumulate.
% Run-length encode the DataIdx:
DataIdx = [1,1,2,1,1,2,2,2,1,1,1];
[val,len] = RunLength(DataIdx);
% Decode back, but with a progressive index, i.e. expand sequential positions by the len of each streak:
subs = RunLength(1:numel(val),len)'
% Accumulate
[val' accumarray(subs, DataPoint)]
PS. RunLength() is on the FEX by Jan Simon.
  댓글 수: 1
Hamad
Hamad 2014년 8월 6일
Thanks very much Oleg!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by