Say I have a matrix A
whose first column contains some item IDs and second column contains either 0 or 1.
A=[3 1
1 0
4 0
3 0
1 1
2 1
3 1
4 0
2 0
4 1
3 1
4 0
2 1
1 1
2 0];
I want to find which item ID has the most 1's, and extract its entries from A, one by one. So, the way I do it is, I make a matrix B
to extract all the 1 entries from A, find the most frequently occurring item ID, freq_item{1}
, in B
, and then extract all entries from A
of that ID. Then, remove all instances of the most frequent item and search for the next most frequent item. If 2 or more items have the same number of 1's, choose the one with the bigger ratio of 1's:
B = A(A(:,2)==1,:);
for i=1:size(unique(A(:,1)),1)
freq_item{i} = A(A(:,1)==mode(B(:,1)),:);
B = B(B(:,1)~=mode(B(:,1)),:);
end
So, the output is:
freq_item{1,1}=[3 1
3 0
3 1
3 1]
freq_item{1,2}=[1 0
1 1
1 1]
freq_item{1,3}=[2 1
2 0
2 1
2 0]
freq_item{1,4}=[4 0
4 0
4 1
4 0]
But this code requires having the overhead of introducing the intermediate matrix B
. Is there a code that can do this without the need for the intermediate matrix B
and is at least as fast as the aforementioned code (i.e., its time complexity is less than or equal to that of the code written above)?