Keeping check of how many times a loop has seen a repeated value
4 views (last 30 days)
I've been playing around with the unique function for a while now, but I can't seem to simply solve this problem.
In my code I've commented and shown what I want to achieve. basicly I've got a matrix where the first 2 columns are unique values (say integer X&Y data) and the last column indicates how often I've come across those values before.
Is there anyone who could point me in the right direction? I've looked at: http://nl.mathworks.com/matlabcentral/answers/122777-how-to-count-repeated-rows-of-characters-using-a-loop and http://nl.mathworks.com/matlabcentral/answers/46085-counting-repeated-values-paired-with-other-repeated-values-and-placing-those-counts-in-an-array
But no avail!
Thanks in advance!
D=[3 1 1]';
B=[2 0 1]';
T_seen=[100 1 1]'; %amount of time these values have been seen before, so the row [3 2] has been seen 100x before
A=[D B T_seen] %old values
A_d=[1 0;1 2;1 2] %new values which need to be added to the old values after removing duplicates
A_d=[A_d ones(size(A_d,1),1)] %adding 1's to the last column to indicate they have been seen once (now)
A_new=[A;A_d] %making one matrix of old and new combined
%with the last column indicating how many times they've been seen
[C, Ia,Ic]=unique(A_new(:,[1:end-1]),'rows'); %removing duplicate values from the matrix. (ignoring the amount of times they've been seen)
display('now. The goal is to create a matrix that looks like this:')
%Here's my failed code:
A0_A_d=[zeros(size(A,1),1);A_d(:,end)] %trying to replaces ones for zeros does not work either.
%New_A_d(indice,end)=New_A_d(indice,end)+1 %does not work either
More Answers (1)
dpb on 4 Jun 2015
I think your counting is off by one in that you need the initialization to be zero until do the counting...consider
Is the correct count for the new rows; you've got to then add for the existing.