计算满足某些条件的矩阵中的列数

时间:2015-02-28 14:20:35

标签: matlab matrix

M = [1022  3001  4451 1022 1022  3001 1022;
      112    45    10  112  112    45  11;
      500    11    55  500  500    11  88;
        2     5     3   99   71    22   2]  



A = M(1:3,:)



B = unique(A','rows')' = [1022        1022        3001        4451
                            11         112          45          10
                            88         500          11          55]

我希望每列B(:,k)k=1:size(B,2)找到M中满足M(4,j)>50的列数。 jB(:,k)中包含M的列向量的可能索引。

C = [1022        1022        3001        4451
       11         112          45          10
       88         500          11          55
        0           2           0           0]    

C(4,:)是理想的结果。

1 个答案:

答案 0 :(得分:1)

这应该适合你 -

threshold = 50;

%// Find unique columns for first three rows of M
[unqM,~,IDs] = unique(M(1:3,:).','rows')  %//'

%// Find the fourth row elements from M that satisfy the threshold
matches = M(end,:)>threshold

%// Get the counts of the unique columns satisfying threshold criteria
out1 = sum(bsxfun(@times,bsxfun(@eq,IDs,1:max(IDs)),matches'),1) %//'
%// OR with HISTC: out1 = histc(IDs.*matches',1:max(IDs)).'

%// Concatenate with the unique columns for the final output
out = [unqM.' ; out1]