-1

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

我有一个矩阵 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 的数字进行处理?

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

我希望我能够解释我。如果是,感谢您的任何建议!

4

1 回答 1

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

编辑:原始帖子已更改,我将不得不重新检查所有内容

于 2013-07-28T21:25:05.360 回答