2013-07-27 156 views
-1

有誰知道如何解決這個問題?Matlab計數兩列出現的次數

我有整數兩列的矩陣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的相應數字(即3465和6843)的數字大於1000的出現次數,我將它們與矩陣B.

我希望我能解釋我。 如果是的話,謝謝你的任何建議!

+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 = [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 

編輯:原帖已經改變了,我將不得不重新審視這一切

+0

謝謝!我編輯了這個問題,並試圖使其更加清晰。確實缺少的數字被NaN取代。我試圖運行一個更大矩陣的代碼。輸出很好。然而,它看起來沒有工作。我沒有找到更高和更低的數字之間的對應關係。爲什麼在第一欄中有NaN? – seli

+0

第一列包含NaN,因爲它是表示行標籤和列標籤交叉點的最佳方式 – Will

+0

非常感謝!我剛剛看到你的答案。我會嘗試代碼並回來。 – seli