MATLAB - 元胞数组中重复次数最多的元素
MATLAB - A Cell Array's Most Repeated Element
我有一个包含 5 个 1x2 数组的元胞数组。有什么办法可以找到重复次数最多的元素吗?我想我不能使用 "mode" 功能。我在互联网上进行了查找,但找不到有关该问题的解决方案。每个人都在谈论带字符串的元胞数组。
我使用的元胞数组是这样的:
{[1 2], [2 5], [3 4], [1 2], [0 4]}
我希望 MATLAB 找到 [1 2] 作为重复次数最多的元素。
提前致谢。
对于统一结构的元胞数组(每个元胞 2 个元素)case
%// Convert the uniformly structured data to a 2D numeric array
Anum = vertcat(A{:})
%// ID unique rows and ID all rows based on those
[~,unqID,ID ] = unique(Anum,'rows')
%// Use 'mode' on ID and then index into unqID to get the index of
%// the most frequently occurring cell and finally index into the
%// input cell array with that index to get the desired output
out = A{unqID(mode(ID))}
因此,对于给定的输入数据 -
A = {[1 2], [2 5], [3 4], [1 2], [0 4]}
你会 -
out =
1 2
包含行向量单元格的更一般情况
如果您要处理的元胞数组在每个元胞中具有任意大小的行向量,则可以使用此技术 -
%// Get all elements of A
A_ele = [A{:}]
%// Get lengths of each cell
lens = cellfun('length',A)
%// Setup a 2D numeric array corresponding to the input cell array
A_num = zeros(max(lens),numel(lens))+max(A_ele)+1
A_num(bsxfun(@ge,lens,[1:max(lens)]')) = A_ele %//'
%// ID each row, find the mode with those & finally have the desired output
[unqrows,unqID,ID ] = unique(A_num.','rows') %//'
out = A{unqID(mode(ID))}
因此,如果您输入为 -
A = {[1 2], [2 5], [3 4], [1 2], [0 4], [1 2 1],[9],[7 2 6 3]}
输出仍然是 -
out =
1 2
这适用于一般元胞数组 A
:
A = {[4 2] [2 5] [4 2] [1 2 1] [9] [7 2 6 3] [1 2 1] [1 2 1] [7 9]}; %// example data
[ii, jj] = ndgrid(1:numel(A)); %// ii, jj describe all pairs of elements from A
comp = cellfun(@isequal, A(ii), A(jj)); %// test each pair for equality
[~, ind] = max(sum(comp,1)); %// get index of (first) most repeated element
result = A{ii(ind)}; %// index into A to get result
我有一个包含 5 个 1x2 数组的元胞数组。有什么办法可以找到重复次数最多的元素吗?我想我不能使用 "mode" 功能。我在互联网上进行了查找,但找不到有关该问题的解决方案。每个人都在谈论带字符串的元胞数组。
我使用的元胞数组是这样的: {[1 2], [2 5], [3 4], [1 2], [0 4]}
我希望 MATLAB 找到 [1 2] 作为重复次数最多的元素。
提前致谢。
对于统一结构的元胞数组(每个元胞 2 个元素)case
%// Convert the uniformly structured data to a 2D numeric array
Anum = vertcat(A{:})
%// ID unique rows and ID all rows based on those
[~,unqID,ID ] = unique(Anum,'rows')
%// Use 'mode' on ID and then index into unqID to get the index of
%// the most frequently occurring cell and finally index into the
%// input cell array with that index to get the desired output
out = A{unqID(mode(ID))}
因此,对于给定的输入数据 -
A = {[1 2], [2 5], [3 4], [1 2], [0 4]}
你会 -
out =
1 2
包含行向量单元格的更一般情况
如果您要处理的元胞数组在每个元胞中具有任意大小的行向量,则可以使用此技术 -
%// Get all elements of A
A_ele = [A{:}]
%// Get lengths of each cell
lens = cellfun('length',A)
%// Setup a 2D numeric array corresponding to the input cell array
A_num = zeros(max(lens),numel(lens))+max(A_ele)+1
A_num(bsxfun(@ge,lens,[1:max(lens)]')) = A_ele %//'
%// ID each row, find the mode with those & finally have the desired output
[unqrows,unqID,ID ] = unique(A_num.','rows') %//'
out = A{unqID(mode(ID))}
因此,如果您输入为 -
A = {[1 2], [2 5], [3 4], [1 2], [0 4], [1 2 1],[9],[7 2 6 3]}
输出仍然是 -
out =
1 2
这适用于一般元胞数组 A
:
A = {[4 2] [2 5] [4 2] [1 2 1] [9] [7 2 6 3] [1 2 1] [1 2 1] [7 9]}; %// example data
[ii, jj] = ndgrid(1:numel(A)); %// ii, jj describe all pairs of elements from A
comp = cellfun(@isequal, A(ii), A(jj)); %// test each pair for equality
[~, ind] = max(sum(comp,1)); %// get index of (first) most repeated element
result = A{ii(ind)}; %// index into A to get result