How to sum by identifier

4 ビュー (過去 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 Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by