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
的列数。 j
是B(:,k)
中包含M
的列向量的可能索引。
C = [1022 1022 3001 4451
11 112 45 10
88 500 11 55
0 2 0 0]
C(4,:)
是理想的结果。
答案 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]