How to plot x-y coordinates which correspond to elements which are repeated at least once

3 visualizaciones (últimos 30 días)
My project concerns capture & recapture of rats. I have a 1040 x 3 array, the first column is the tag number of the rat (when each rat is captured for the first time, its given a tag (harmlessly of course!) the other 2 columns are the x & y coordinates respectively of where in a 7x7 plane the rat was captured. If the same rat is captured again, its position is then recorded again. I want to plot how the position of all of the individual rats which were captured more than once change over time. I've got a list of the indices of all of the non-repeated elements (i.e. all the rats which were seen once, but not again), but don't know where to go from there. Thanks for any help
  3 comentarios
Timothy Russell
Timothy Russell el 4 de Dic. de 2012
Editada: Walter Roberson el 27 de Dic. de 2012
4641 6 1
4641 3 2
4641 6 2
1456 2 6
1456 1 5
1508 1 7
There's an example. 1st Column is the rats tag number, 2nd is the x-coordinate (in this 7x7 stake where they were caught) and 3rd is the y-coord. Basically what I want to do is plot the trajectory of any rats which were caught more than once. So every time the same number in the first column appears, I want matlab to grab the data from the other 2 columns and plot the points, only joining the points of the same rats. I don't want any points on the plot of rats only caught once. So its as if the plot is of the rats moving through time. Hope this helps, thanks!
Timothy Russell
Timothy Russell el 4 de Dic. de 2012
4641 6 1
4641 3 2
4641 6 2
1456 2 6
1456 1 5
1508 1 7
Sorry, I copied the array pretty wonkily last time, here it is more clearly.

Iniciar sesión para comentar.

Respuesta aceptada

John Petersen
John Petersen el 29 de Nov. de 2012
This may get you started
A = 1040x3 array
[r,c] = size(A);
[tags, iA,ic] = unique(A(:,1)); % rats that have been captured
t = 1:r; % scale time as needed.
rat2 = setdiff(tags,iA); % rats that have been captured more than once
colr = ['b','g','r','m','c','k']; % identify rats with different colors
figure;
for i=1:length(rat2)
ind = A(:,1)==A(rat2(i),1); % pick all the rat locs for rat i
plot3(t(ind),A(ind,2),A(ind,3),colr(i)); % plot rat i trajectory
hold on;
end
grid on;
  9 comentarios
John Petersen
John Petersen el 5 de Dic. de 2012
The dimensions I was user were, time, x, and y. But if all you want is x,y, then try what I have below. I also fixed the last little problem I think.
TAGS = [1:6]';
A = [ TAGS(ceil(6*rand(20,1))) rand(20,2)];
[r,c] = size(A);
[tags, iA,ic] = unique(A(:,1)); % rats that have been captured
t = [1:r]'; % scale time as needed.
niA = setdiff(t,iA);
[rat2,i2] = intersect(A(niA,1),A(iA,1)); % rats that have been captured more than once
rat2ind = [iA;niA(i2)]; % indices of rats seen 2+ times
colr = ['bo-';'go-';'ro-';'mo-';'co-';'ko-']; % identify rats with different colors
figure;
for i=1:length(rat2)
ind = A(:,1)==rat2(i); % pick all the rat locs for rat i
plot(A(ind,2),A(ind,3),colr(i,:)); % plot rat i trajectory
hold on;
end
Timothy Russell
Timothy Russell el 27 de Dic. de 2012
Sorry for my late reply, had other projects on. This solution worked excellently, thanks!

Iniciar sesión para comentar.

Más respuestas (1)

Timothy Russell
Timothy Russell el 27 de En. de 2013
This worked perfectly, but I was wondering, is there an easy way to, instead of plotting the result, saving the result in an (n,3) matrix where n is the number of data points which would have been plotted originally. This is what I tried in the for loop: for i=1:length(rat2ind) ind = A(:,1)==A(rat2ind(i),1); H=vertcat(H,[A(ind,2),A(ind,3)]); end But it gives me an array with more entries than my original data had (which is impossible if its working). It must be this part which isn't working though as i've checked the plotting function loads and its working fine. Thanks
  2 comentarios
Timothy Russell
Timothy Russell el 27 de En. de 2013
for i=1:length(rat2ind)
ind = A(:,1)==A(rat2ind(i),1);
H=vertcat(H,[A(ind,1),A(ind,2),A(ind,3)]);
end
John Petersen
John Petersen el 29 de En. de 2013
Isn't "A" the nx3 matrix you're looking for? or am I missing something?

Iniciar sesión para comentar.

Categorías

Más información sobre Graph and Network Algorithms en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by