-1

この問題を解決する方法を知っている人はいますか?

1000 より大きいまたは小さい整数の 2 つの列を持つ行列 A があります。

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

そして、1000 より大きい数値と 1000 より小さい数値の間の可能な対応を示す 2 番目の行列 B があります。

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

次の2つの行列を取得したいと思います。

最初の結果は、行列 A の出現回数を示します。

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

2 番目の結果は、1000 より大きい数値のみを持つマトリックス A の行を取得し、マトリックス 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未満のすべての数を一度に行うことはできますか?

最後に、問題の 2 番目の部分では、1000 未満の対応する数を持たない行列 A の 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

2番目の結果に対して私ができる最善のこと:

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 でなければなりません。
  • 第 2 に、最後の行列がわかりません。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 に答える