How to sum data
5 ビュー (過去 30 日間)
古いコメントを表示
Hi, my data has the following format:
raw_headers={'D' 'A' 'D' 'B' 'D' 'D'}
raw_data=[5 1 10 1 15 20]
And and want final results that way:
format_headers={'A' 'B' 'C' 'D' }'
format_data=[1;1;0;50]
I have issue doing the summation wihtout a loop for all unique headers.
Any idea?
0 件のコメント
回答 (4 件)
Geoff Hayes
2014 年 8 月 5 日
Since your raw_headers does not necessarily contain all headers (in this case, C is missing) then you could try the following where you define the full list of (unique) headers ahead of time, and iterate over that list
headerData = {'A','B','C','D'};
numChars = length(headerData);
format_data = zeros(numChars,1);
at = 1;
for k=headerData
idcs = find(strcmpi(raw_headers(:),k));
if ~isempty(idcs)
format_data(at) = sum(raw_data(idcs));
end
at = at + 1;
end
So we just iterate over each possible header and grab the indices of each raw header that matches it. Then just sum the raw data given that set of indices. Try the above and see what happens!
2 件のコメント
Image Analyst
2014 年 8 月 5 日
Don't be afraid of a loop so tiny. I mean, it's not like you have tens of millions of these cells, do you? Anyway, I don't see how you're getting the output from the input. Maybe the letter headers are just going from min 'A' to max 'D', but what about the numbers? Is it a histogram?
dpb
2014 年 8 月 5 日
Easiest will be to augment the header row w/ all values wanted. If do so, then
>> raw_headers={'D' 'A' 'D' 'B' 'D' 'D' 'C'};
>> [~,~,ib]=unique(raw_headers)
ib =
4 1 4 2 4 4 3
>> accumarray(ib.',[raw_data zeros(length(ib)-length(raw_data),1)])
ans =
1
1
0
50
>>
2 件のコメント
dpb
2014 年 8 月 5 日
編集済み: dpb
2014 年 8 月 5 日
Didn't do something correctly, as you can see I pasted the results directly from the command line.
That error would be true if you used your original data array with the augmented headers. That's the point of the added zeros in the above; to augment to the same length.
Just to be sure, here it is again...
>> raw_headers={'D' 'A' 'D' 'B' 'D' 'D' 'C' };
>> [~,~,ib]=unique(raw_headers)
ib =
4 1 4 2 4 4 3
>> accumarray(ib.',[raw_data zeros(length(ib)-length(raw_data),1)])
ans =
1
1
0
50
>>
Just fine, as advertised. Now don't augment the data array...
>> accumarray(ib.',raw_data)
Error using accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
>>
And, yes, Virginia, if you don't add entries for the additional headings, "Houston, we have a problem!"
Ergo, the augmentation is important.
Show your work.
Nade Sritanyaratana
2014 年 8 月 5 日
The following code uses raw_headers to index subscripts for accumarray, creates a secondary subscript based on all characters between the minimum (e.g., A) to the maximum (e.g. D), grabs only the values of accumarray that correspond to the secondary subscript, and then reformats the data to the desired output:
raw_headers={'D' 'A' 'D' 'B' 'D' 'D'};
raw_data=[5 1 10 1 15 20];
% Create a numerical subscript of the characters
subs = double(cell2mat(raw_headers'));
% Keep track not just of A, B, and D, but also C, by creating a secondary subscript
subs2 = min(subs):max(subs);
% Sum up arrays according to matching subs (corresponding to matching chars)
A = accumarray(subs,raw_data',[],[]);
% Grab only the sums from A to D
A = A(subs2);
% reformat outputs
format_headers = cellstr(char(subs2)')';
format_data = A';
0 件のコメント
Andrei Bobrov
2014 年 8 月 5 日
編集済み: Andrei Bobrov
2014 年 8 月 5 日
s = unique(raw_headers);
format_headers = cellstr((s{1}:s{end})');
[~,ii] = ismember(raw_headers,format_headers);
format_data = accumarray(ii(:),raw_data(:));
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!