這裏是第一個結果的代碼:
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
編輯:原帖已經改變了,我將不得不重新審視這一切
那麼你嘗試過什麼? –