[英]Matlab: How I can make this transformation on the matrix A?
我有一個矩陣A
4x10000
,我想用它來找到另一個矩陣C
我將用一個簡單的例子簡化我的問題:
從矩陣A
20 4 4 74 20 20
36 1 1 11 36 36
77 1 1 15 77 77
3 4 2 6 7 8
我想首先找到一個中間實體B:
2 3 4 6 7 8
[20 36 77] 0 1 0 0 1 1 3
[4 1 1] 1 0 1 0 0 0 2
[74 11 15] 0 0 0 1 0 0 1
如果第一行的對應值和左邊的向量,我們放1,在矩陣A中做一列。
實體B的最后一列是每行1的總和。
最后,我想要一個矩陣C,它由留在實體B中的向量組成,但只有當1的和大於或等於2時才有。
對於我的例子:
20 4
C = 36 1
77 1
注意:對於我的問題,我使用矩陣A
4x10000
看看這是否適合你 -
%// We need to replace this as its not available in your old version of MATLAB:
%// [unqcols,~,col_match] = unique(A(1:end-1,:).','rows','stable') %//'
A1 = A(1:end-1,:).'; %//'
[unqmat_notinorder,row_ind,labels] = unique(A1,'rows');
[tmp_sortedval,ordered_ind] = sort(row_ind);
unqcols = unqmat_notinorder(ordered_ind,:);
[tmp_matches,col_match] = ismember(labels,ordered_ind);
%// OR use - "[tmp2,col_match] = ismember(A1,out,'rows');"
C = unqcols(sum(bsxfun(@eq,col_match,1:max(col_match)),1)>=2,:).'; %//'
%// OR use - "C = out(accumarray(col_match,ones(1,numel(col_match)))>=2,:).'"
這應該工作:
[a,~,c] = unique(A(1:end-1,:).', 'rows', 'stable');
C=a(histc(c,unique(c))>=2, :).';
編輯:對於舊版本的MATLAB:
D=A(1:end-1,:);
C=unique(D(:,squeeze(sum(all(bsxfun(@eq, D, permute(D, [1 3 2])))))>=2).', 'rows').':
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.