How to sum by identifier

조회 수: 5 (최근 30일)
QuanCCC
QuanCCC 2018년 9월 16일
편집: QuanCCC 2018년 9월 25일
Hi there,
I have a table data: A column is the identifier data, column B C D is the data I want to sum up by the same A value.
How can I do that?
I tried 'accumarray', but it seems I mixed table and array. MATLAB says"Undefined function 'accumarray' for input arguments of type 'table'." Then I turned my data to an array, MATLAB says "All input arguments must be tables." And I do have 3 columns values to be summed up. Thank you.
[a,~,c] = unique(yourdata(:,1)); % 'yourdata(:,1)' 1 means column 1 is the identifier
output = [a, accumarray(c,yourdata(:,2))]; % 2 means the colume to be summed up.
  댓글 수: 1
Walter Roberson
Walter Roberson 2018년 9월 16일
yourdata{:, 1} instead of (:, 1)

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

채택된 답변

Steven Lord
Steven Lord 2018년 9월 16일
Use groupsummary with A as the grouping variable.
  댓글 수: 1
QuanCCC
QuanCCC 2018년 9월 25일
편집: QuanCCC 2018년 9월 25일
If the sum up is for all data with the same identifier, a groupsummary is the best idea. If you have more data (more than needed for the sum up) with the same identifier, the codes I mentioned works.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by