Matlab计算两列的出现次数

时间:2013-07-27 22:35:42

标签: matlab count

有谁知道如何解决这个问题?

我有一个矩阵A,有两列整数,可以大于或小于1000。

  A=[4565,345;325,6843;4565,4565;321,9876;6843,321;6843,321;6843,6843;9876,9876;6843,9876]

我有第二个矩阵B,表示数字大于和小于1000的可能对应关系:

 B= [9876,321;6843,532;6843,325;4565,345]

我想获得以下两个矩阵。

第一个结果表示矩阵A中的出现次数:

 9876 321 1
 6843 321 2
 6843 325 1
 4565 345 1

第二个结果采用矩阵A的行,这些行的数字大于1000,并表示基于矩阵B的可能出现次数。

9876 321 2
6843 532 2
6843 325 2
4565 345 1

为了解决问题的第一部分,我创建了一个这种类型的数组C:

C{1,1}=325   C{1,2}=6843
C{2,1}=321   C{2,2}=[9876,6843,6843]
C{3,1}=345   C{3,2}=4565

如何转换此数组以获得类似第一个结果的矩阵? 我正在使用此代码:

% find the unique elements in the input
uniqueBB=unique(finalA{1,2})';

%  find the occurences of this unique number in the data:
[~,uniq_id]=ismember(finalA{1,2},uniqueBB);

% count how many times each unique word is found:
uniq_BB_num = arrayfun(@(x) sum(uniq_id==x),1:numel(uniqueBB));

%transpose
uniqueBB = transpose(uniqueBB);
uniq_BB_num = transpose(uniq_BB_num);

counts=[uniqueBB,uniq_BB_num];

对于所有小于1000的数字,它可以一次完成吗?

最后,对于问题的第二部分,我将计算大于1000的数字的出现次数,矩阵A没有相应的数字小于1000(即3465和6843),我将它们与矩阵相乘乙

我希望我能解释一下。 如果是,请感谢任何建议!

1 个答案:

答案 0 :(得分:1)

以下是第一个结果的代码:

A = [4565,345;325,6843;4565,4565;321,9876;6843,321;6843,321;6843,6843;9876,9876;6843,9876]
A_cols = {num2cell(A(:,1)'),num2cell(A(:,2)')};
A_rows = cellfun(@(varargin)[varargin],A_cols{:},'un',0);
A_flippedlo_rows = cellfun(@(x) feval(@(varargin) varargin{3-varargin{1}}(), x{1}<x{2}, fliplr(x), x), A_rows, 'un', 0);

boolvec = cell2mat(cellfun(@(x) bitand(x{1}>1000, x{2}<1000),A_flippedlo_rows,'un',0));
A_hilo_rows = A_flippedlo_rows(boolvec);
A_hilo_cols = cellfun(@(varargin)[varargin],A_hilo_rows{:},'un',0);
A_hilo = [cell2mat(A_hilo_cols{1});cell2mat(A_hilo_cols{2})]'
[A_hilo_unique, iA_hilo, iA_hilo_unique] = unique(A_hilo, 'rows');
firstresult = [A_hilo_unique accumarray(iA_hilo_unique, ones(1,length(A_hilo)))]

A =
    4565         345
     325        6843
    4565        4565
     321        9876
    6843         321
    6843         321
    6843        6843
    9876        9876
    6843        9876

A_hilo =
    4565         345
    6843         325
    9876         321
    6843         321
    6843         321

firstresult =
    4565         345           1
    6843         321           2
    6843         325           1
    9876         321           1

我能为第二个结果做得最好:

boolvec = cell2mat(cellfun(@(x) bitand(x{1}>1000, x{2}>1000),A_flippedlo_rows,'un',0));
A_hihi_rows = A_flippedlo_rows(boolvec);
A_hihi_cols = cellfun(@(varargin)[varargin],A_hihi_rows{:},'un',0);
A_hihi = [cell2mat(A_hihi_cols{1});cell2mat(A_hihi_cols{2})]'
A_hihi_joinkeys = cell2mat(A_hihi_cols{2});

B = [9876,321;6843,532;6843,325;4565,345]
Bkeys = B(:,1)';
Bvals = B(:,2)';
[Bkeys_unique,ia,ic] = unique(Bkeys);
if ~iscolumn(ic) %Matlab prior to 2013 did not output a column vector
 ic = ic';
end
Bvals_grouped = accumarray(ic,Bvals,{},@(x) {sort(x)})';
Bvals_grouped = cellfun(@(x) num2cell(x'), Bvals_grouped, 'un', 0);
[Lia, iBkeys_unique] = ismember(A_hihi_joinkeys,Bkeys_unique);

A_hihi_cols{2} = Bvals_grouped(iBkeys_unique);
A_hihi_rows = cellfun(@(varargin)[varargin],A_hihi_cols{:},'un',0);

A_hihi_expanded_rows = cellfun(@(x) {repmat({x{1}},1,length(x{2})),x{2}}, A_hihi_rows, 'un', 0);
A_hihi_expanded_rows = cellfun(@(x) cellfun(@(varargin)[varargin],x{:},'un',0), A_hihi_expanded_rows, 'un', 0);
A_hihi_expanded_rows = [A_hihi_expanded_rows{:}];
A_hihi_expanded_cols = cellfun(@(varargin)[varargin],A_hihi_expanded_rows{:},'un',0);
A_hihi_expanded = [cell2mat(A_hihi_expanded_cols{1});cell2mat(A_hihi_expanded_cols{2})]'
[A_hihi_expanded_unique, iA_hihi_expanded, iA_hihi_expanded_unique] = unique(A_hihi_expanded, 'rows');
secondresult = [A_hihi_expanded_unique accumarray(iA_hihi_expanded_unique, ones(1,length(A_hihi_expanded_unique)))]


A_hihi =

    4565        4565
    6843        6843
    9876        9876
    9876        6843


B =

    9876         321
    6843         532
    6843         325
    4565         345


A_hihi_expanded =

    4565         345
    6843         325
    6843         532
    9876         321
    9876         325
    9876         532


secondresult =

    4565         345           1
    6843         325           1
    6843         532           1
    9876         321           1
    9876         325           1
    9876         532           1

将数组C转换为聚合计数矩阵:

C{1,1}=325; C{1,2}=6843; 
C{2,1}=321; C{2,2}=[9876,6843,6843];
C{3,1}=345; C{3,2}=4565;
C
C_cols = {C(:,1)' C(:,2)'}; %convert to nested cells
C_cols{2} = cellfun(@(x) num2cell(x), C_cols{2}, 'un', 0);
C_rows = cellfun(@(varargin)[varargin],C_cols{:},'un',0); %transpose to rows
C_rows = cellfun(@(x) {repmat({x{1}},1,length(x{2})),x{2}}, C_rows, 'un', 0); %repeat lo to length(hi)
C_rows = cellfun(@(x) cellfun(@(varargin)[varargin],x{:},'un',0), C_rows, 'un', 0); %mix lo and 
C_rows = [C_rows{:}]; %expand
C_cols = cellfun(@(varargin)[varargin],C_rows{:},'un',0); %transpose to cols
C_mat = [cell2mat(C_cols{2});cell2mat(C_cols{1})]';
[C_mat_unique, iC_mat, iC_mat_unique] = unique(C_mat, 'rows');
C_out = [C_mat_unique accumarray(iC_mat_unique, ones(1,length(C_rows)))]

C = 
[325]    [      6843]
[321]    [1x3 double]
[345]    [      4565]


C_out =
    4565         345           1
    6843         321           2
    6843         325           1
    9876         321           1

以下是原帖的旧回复

我无法理解你的一些帖子:

  • 首先,显示的矩阵不能包含空格,它们必须为0或NaN
  • 其次,我不理解最后一个矩阵 - 如果A被过滤为高整数,那么输出不能有低整数标题或者它们的所有计数都为零

以下代码应生成另一个结果矩阵:

A = [4565 345;325 6843;4565 4565;321 9876;6843 321;6843 321;6843 6843;9876 9876;6843 nan;nan 9876];
a_cols = {num2cell(A(:,1)'),num2cell(A(:,2)')};
a_rows = feval(@(x) cellfun(@(varargin)[varargin],x{:},'un',0),a_cols);
a_rows_hilo = cellfun(@(x) feval(@(varargin) varargin{3-varargin{1}}(), x{1}<x{2}, fliplr(x), x), a_rows, 'un', 0);
bm = cell2mat(cellfun(@(x) bitand(x{1}>1000, x{2}<1000),a_rows_hilo,'un',0));
a_rows_valid = a_rows_hilo(bm);
a_cols_valid = feval(@(x) cellfun(@(varargin)[varargin],x{:},'un',0),a_rows_valid);
[labels_hi,ia,subs(:,1)] = unique(cell2mat(a_cols_valid{1}));
[labels_lo,ia,subs(:,2)] = unique(cell2mat(a_cols_valid{2}));
agg_count = accumarray(subs,ones(1,length(a_rows_valid)));
agg_count = [[NaN labels_hi]' [labels_lo;agg_count]]

agg_count = 
 NaN         321         325         345
4565           0           0           1
6843           2           1           0
9876           1           0           0
编辑:原帖已更改,我将不得不重新审核