MATLAB Answers

QuanCCC
0

How to sum by identifier

Asked by QuanCCC
on 16 Sep 2018 at 6:14
Latest activity Answered by Steven Lord
on 16 Sep 2018 at 14:23

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 Comment

yourdata{:, 1} instead of (:, 1)

Sign in to comment.

1 Answer

Answer by Steven Lord
on 16 Sep 2018 at 14:23

Use groupsummary with A as the grouping variable.

  0 Comments

Sign in to comment.