group by in Matlab to find the value that resulted minimum similar to SQL -
i have dataset having columns a, b, c , d want group dataset a,b , find c such d minimum each group can "group by" using 'grpstats" :
grpstats(m,[m(:,1) m(:,2) ],{'min'});
i don't know how find value of m(:,3) resulted min in d
in sql suppose use nested queries , use primary keys. how can solve in matlab?
here example:
>> m =[4,1,7,0.3; 2,1,8,0.4; 2,1,9,0.2; 4,2,1,0.2; 2,2,2,0.6; 4,2,3,0.1; 4,3,5,0.8; 5,3,6,0.2; 4,3,4,0.5;] >> grpstats(m,[m(:,1) m(:,2)],'min') ans = 2.0000 1.0000 8.0000 0.2000 2.0000 2.0000 2.0000 0.6000 4.0000 1.0000 7.0000 0.3000 4.0000 2.0000 1.0000 0.1000 4.0000 3.0000 4.0000 0.5000 5.0000 3.0000 6.0000 0.2000
but m(1,3) , m(4,3) wrong. correct answer looking is:
2.0000 1.0000 9.0000 0.2000 2.0000 2.0000 2.0000 0.6000 4.0000 1.0000 7.0000 0.3000 4.0000 2.0000 3.0000 0.1000 4.0000 3.0000 4.0000 0.5000 5.0000 3.0000 6.0000 0.2000
to conclude, don't want minimum of third column; want it's values corresponding minimum in 4th column
grpstats
won't this, , matlab doesn't make easy might hope.
sometimes brute force best, if doesn't feel great matlab style:
[b,m,n]=unique(m(:,1:2),'rows'); =1:numel(m) idx=find(n==i); [~,subidx] = min(m(idx,4)); a(i,:) = m(idx(subidx),3:4); end >> [b,a] ans = 2 1 9 0.2 2 2 2 0.6 4 1 7 0.3 4 2 3 0.1 4 3 4 0.5 5 3 6 0.2
Comments
Post a Comment