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

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

Can you post a sample(6x3) of your data, and explain what is the result expected?
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!
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

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

Ah thanks, I ran your suggestion and got this error: Subscript indices must either be real positive integers or logicals.
Error in Untitled3 (line 8) ind = A(:,1)==A(rat2(i),1); % pick all the rat locs for rat i
Only have a very basic understanding of matlab, but I don't see what the problem here is, you've defined i in the for loop (and its definitely only contains real, positive numbers) so I don't know what its talking about. Thanks anyway, any more help would be greatly appreciated!!
You might need a test on rat2 to see if there are ANY rats that were seen twice. So right before the for loop you could put this condition
if ~isempty(rat2)
for loop
end
Ah thanks again, but as you can see from the example 6x3 array I've posted above, some of the rats were definitely caught more than once as tags 4641 & 1456 both appear at least twice in the top 6 entries. I didn't include the other part of the error message last time (stupidly) which was: Attempted to access A(0,1);
However, after getting the error I typed in: >> rat2(i)
ans =
0
So i'm guessing that rat2 is empty...
The error is here, I think (can't try it right now):
[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
iA contains the indices of the first times each unique tag occurs in column one, tags on the other hand contains the actual tag data.
So substitute those 3 lines by
[tags, iA,ic] = unique(A(:,1)); % rats that have been captured
t = 1:r; % scale time as needed.
rat2 = A(setdiff(t,iA),1); % rats that have been captured more than once
The code should now run.
Now it gives me: Attempted to access A(4641,1); index out of bounds because size(A)=[1040,3]. Error in Untitled (line 8) ind = A(:,1)==A(rat2(i),1); % pick all the rat locs for rat i
The number 4641 is the first entry of the matrix A, i.e. the tag number of the first rat, just in case that information helps at all...thanks a lot for your help so far anyway though, it and any more you give is much appreciated!
How about this -- Note: you'll have to deal with colors, I've only shown 6 different colors to use.
[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(rat2ind)
ind = A(:,1)==A(rat2ind(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
Sorry, should have posted the reply here!
Don't worry about the colours, I can sort that out/I doubt my supervisor will mind.
I now get a 3-d plot of 6 different trajectories, but I also get this error:
Index exceeds matrix dimensions.
Error in Untitled3 (line 11) plot3(t(ind),A(ind,2),A(ind,3),colr(i,:)); % plot rat i trajectory
Also, I was hoping for just a 2-d plot, and am not quite sure what the 3rd dimension is representing! It looks cool though. Thanks again!
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
Sorry for my late reply, had other projects on. This solution worked excellently, thanks!

Iniciar sesión para comentar.

Más respuestas (1)

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

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
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 Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by