How to consolidate rows with same value, while adding their cells?

조회 수: 2 (최근 30일)
Ethan
Ethan 2013년 11월 12일
편집: Azzi Abdelmalek 2013년 11월 12일
I have NOAA climate data reported every 15 minutes and need to consolidate it into daily info. I want to add cells at the end of a row together that have the same date, and make a new matrix with the 'summed' and consolidated info. Here is a rough example of what I want to do:
FROM:
[date, precip
2/1, 4
2/1, 5
2/1, 6
2/2, 2
2/2, 3
2/2, 1
2/2, 4]
TO:
[date, precip
2/1, 15
2/2, 10]

채택된 답변

Doug Hull
Doug Hull 2013년 11월 12일
clear
d = {'date', 'precip'; '2/1', 4; '2/1', 5; '2/1', 6; '2/2', 2; '2/2', 3; '2/2', 1; '2/2',4}
dates = {d{2:end,1}};
amount = [d{2:end,2}];
[uniqueDates, firstInd, allInd] = unique(dates);
for i = 1:numel(firstInd)
totalRain(i) = sum(amount(allInd == i));
end
disp(uniqueDates)
disp(totalRain)
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 12일
편집: Azzi Abdelmalek 2013년 11월 12일
Ethan commented
Thanks a bunch! working like a charm.

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 11월 12일
편집: Azzi Abdelmalek 2013년 11월 12일
s = { '2/1', 4; '2/1', 5; '2/1', 6; '2/2', 2; '2/2', 3; '2/2', 1; '2/2',4}
[a,b,c]=unique(s(:,1),'stable')
out=[a num2cell(accumarray(c,cell2mat(s(:,2))))]

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by