필터 지우기
필터 지우기

reduce matrix data by combining values

조회 수: 1 (최근 30일)
Christoph Meier
Christoph Meier 2016년 3월 1일
댓글: Walter Roberson 2016년 3월 1일
Dear matlab community,
I need to reduce data in a matrix of two columns, and many rows (almost 2 billion, so I would like to find a way to automate it!)
The structure is as follows:
Column 1 has a bunch of discrete values, which occur repeatedly.
Example:
a
b
a
c
b
Column 2 has values in it, that should be added up for all "a", all "b", and all "c", that only one "a", "b", "c" is left in the final matrix.
Example:
Initial matrix:
a 10
b 5
a 1
c 20
b 3
The final matrix should look like this:
a 11
b 8
c 20
Thank you very much! A solution would help me a lot!

채택된 답변

Walter Roberson
Walter Roberson 2016년 3월 1일
In the below code I assume your array is a cell array (because you cannot mix letters and numbers in a numeric array).
[unique_keys, ~, idx] = unique(YourArray(:,1));
vals_to_total = cell2mat( YourArray(:,2) );
totals = accumarray(idx, vals_to_total);
results = [unique_keys, num2cell(totals)];
  댓글 수: 3
Christoph Meier
Christoph Meier 2016년 3월 1일
I came across a problem:
I replaced YourArray with my actual array "in10", the 2 columns, 2b rows matrix:
[unique_keys, ~, dx] = unique(in10(:,1));
vals_to_total = cell2mat( in10(:,2));
totals = accumarray(idx,vals_to_total);
results = [unique_keys,num2cell(totals)];
However, I get the following error message:
Error in unique>uniqueR2012a (line 542)
groupsSortA = [true; groupsSortA]; % First
element is always a member of unique list.
Error in unique (line 88)
[varargout{1:nlhs}] = uniqueR2012a(varargin{:});
Do you know what the problem may be?
Thank you!
Walter Roberson
Walter Roberson 2016년 3월 1일
If your array is numeric instead of a cell array, then
[unique_keys, ~, idx] = unique(YourArray(:,1));
vals_to_total = YourArray(:,2);
totals = accumarray(idx, vals_to_total);
results = [unique_keys, totals];
You did not show enough of the error message for me to see what it is complaining about.
Is it possible that your data is a MATLAB table() or dataset() data type, or something other than numeric or a cell array?
Also watch out: you assigned to "dx" in the unique() line, but you use "idx" in the accumaray()

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by