Matlab:如何在矩阵A上进行此转换? [英] Matlab: How I can make this transformation on the matrix A?
问题描述
我有一个矩阵A
4x10000
,我想用它找到另一个矩阵C
.
I have a matrix A
4x10000
, I want to use it to find another matrix C
.
我将通过一个简单的例子来简化我的问题:
I'll simplify my problem with a simple example:
来自矩阵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:
I want, first, to find an intermediate entity 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的一列.
we put 1 if the corresponding value of the first line and the vector on the left, made a column in the matrix A.
实体B的最后一列是每行1的总和.
the last column of the entity B is the sum of 1 of each line.
最后,我想要一个矩阵C,该矩阵C由留在实体B中的矢量组成,但前提是1的总和大于或等于2.
at the end I want a matrix C, consisting of vectors which are left in the entity B, but only if the sum of 1 is greater than or equal to 2.
以我的示例为例:
20 4
C = 36 1
77 1
N.B:对于我的问题,我使用矩阵A
4x10000
N.B: for my problem, I use a matrix 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,:).'"
这篇关于Matlab:如何在矩阵A上进行此转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!