在 Matlab 上聚合 Table
Aggregation on Matlab Table
我在 matlab 中有以下 table,我想根据列 name ,
聚合这个 table
name nums
'es' '1,2,3'
'mo' '4,3,1'
'al' '2,3,1'
'es' '40,2,8'
'es' '1,2,5'
'mo' '5,2,1'
'ta' '9,4,2'
'al' '2,6,1'
...
这是我想要的输出(nums 应该是唯一的):
name nums
'es' '1,2,3,8,40,5'
'mo' '4,3,1,5,2'
'al' '2,3,1,6'
'ta' '9,4,2'
...
这是我的代码,
n,m = size(T);
for i = 1:n
if ~ismember(name,mynewtab)
mynewtab.input(i).nums = mynewtab.input(i).nums + nums;
else
mynewtab.input(i).nums = mynewtab.input(i).nums + nums;
mynewtab.input(i).name = name;
end
end
但是这段代码有一些错误。
"This code has some errors" 不是一个很好的问题陈述,您应该从 +
没有像您认为的字符数组那样定义这一事实开始。
使用 strjoin
和 unique
的代码应该可以满足您的需求....
uNames = unique(tbl.name); % Get unique names
c = cell( numel(uNames), 2 ); % Set up output (we'll turn this into a table later)
for ii = 1:numel(uNames)
c{ii,1} = uNames{ii}; % Assign the name to 1st column
% Join all strings in the 'nums' column, with a comma between them, when the
% value in the names column is equal to uNames{ii}
c{ii,2} = strjoin( tbl.nums( strcmp( tbl.name, uNames{ii} ) ).', ',' );
end
tblOut = cell2table( c, 'VariableNames', {'name','nums'} );
如果您只想从字符串中获取唯一元素,则必须使用 strsplit
以逗号分隔,然后在调用 unique
后连接在一起...将 c{ii,2} = ...
行替换为以下内容:
vals = tbl.nums( strcmp( tbl.name, uNames{ii} ) ).'; % Get nums for this name
vals = cellfun( @(str)strsplit(str,','), vals, 'uni', 0 ); % Split all on ','
% Join the list of unique values back together.
% Could use 'stable' argument of unique for order preservation.
c{ii,2} = strjoin( unique( [vals{:}] ), ',' );
注意:如果将数字列表存储为实际数字数组而不是字符数组,这一切都会容易得多!
我在 matlab 中有以下 table,我想根据列 name ,
聚合这个 tablename nums
'es' '1,2,3'
'mo' '4,3,1'
'al' '2,3,1'
'es' '40,2,8'
'es' '1,2,5'
'mo' '5,2,1'
'ta' '9,4,2'
'al' '2,6,1'
...
这是我想要的输出(nums 应该是唯一的):
name nums
'es' '1,2,3,8,40,5'
'mo' '4,3,1,5,2'
'al' '2,3,1,6'
'ta' '9,4,2'
...
这是我的代码,
n,m = size(T);
for i = 1:n
if ~ismember(name,mynewtab)
mynewtab.input(i).nums = mynewtab.input(i).nums + nums;
else
mynewtab.input(i).nums = mynewtab.input(i).nums + nums;
mynewtab.input(i).name = name;
end
end
但是这段代码有一些错误。
"This code has some errors" 不是一个很好的问题陈述,您应该从 +
没有像您认为的字符数组那样定义这一事实开始。
使用 strjoin
和 unique
的代码应该可以满足您的需求....
uNames = unique(tbl.name); % Get unique names
c = cell( numel(uNames), 2 ); % Set up output (we'll turn this into a table later)
for ii = 1:numel(uNames)
c{ii,1} = uNames{ii}; % Assign the name to 1st column
% Join all strings in the 'nums' column, with a comma between them, when the
% value in the names column is equal to uNames{ii}
c{ii,2} = strjoin( tbl.nums( strcmp( tbl.name, uNames{ii} ) ).', ',' );
end
tblOut = cell2table( c, 'VariableNames', {'name','nums'} );
如果您只想从字符串中获取唯一元素,则必须使用 strsplit
以逗号分隔,然后在调用 unique
后连接在一起...将 c{ii,2} = ...
行替换为以下内容:
vals = tbl.nums( strcmp( tbl.name, uNames{ii} ) ).'; % Get nums for this name
vals = cellfun( @(str)strsplit(str,','), vals, 'uni', 0 ); % Split all on ','
% Join the list of unique values back together.
% Could use 'stable' argument of unique for order preservation.
c{ii,2} = strjoin( unique( [vals{:}] ), ',' );
注意:如果将数字列表存储为实际数字数组而不是字符数组,这一切都会容易得多!