# Inter individual distance between fishes

3 Ansichten (letzte 30 Tage)
Shramana am 22 Mär. 2024
Bearbeitet: Divyanshu am 2 Apr. 2024
I want to calculate inter-individual distances between 5 fishes. I extracted the trajectories and now I have X and Y coordinates of the those trajectories. I have a code with which I could calculate inter-individual distances between fishes for a single X and Y fragment only but there are multiple fragments for a signle video and I have many videos. Hence the code need to run in loop. Can anyone one provide a code which will run in loop for multiple fragments?
##### 2 KommentareKeine anzeigenKeine ausblenden
Matt J am 22 Mär. 2024
Can't you just wrap the code you already have in a loop?
Shramana am 26 Mär. 2024
I tried but it is not working. This is the code,
% xy = randn(5,2);
whos
nframes = size(x, 1);
dmean=zeros(nframes, 1);
for i=1:nframes
xy = [x(i,:)', y(i, :)'];
d = pdist2(xy, xy);
% extract the lower triangle
[ii, jj] = meshgrid(1:5, 1:5);
d1 = d(ii>jj); % d21, d31, ..., d61, d32, .., d62, ... d65
dmean(i) = mean(d1);
end
plot(1:nframes, dmean)
xlabel('frame number');
ylabel('mean distance')

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Divyanshu am 2 Apr. 2024
Bearbeitet: Divyanshu am 2 Apr. 2024
Hi Shramana,
I am assuming that the code provided in the comments works fine and gives the desired results for X-Y coordinates of 1 fragment of a video. Here is the sample code to extend it to work for multiple fragments and multiple videos:
%The outermost loop iterates over all the videos I have assumed we have 10
%videos
for vid=1:10
%This is just a sample loop which iterates over 10 videos, you may need to modify the
%logic within each iteration and also the path to coordinate-files may get changed for
%each iteration, that logic you may need to incorporate in this sample code.
%Below loop iterates over all the frames/fragments of a single video 'vid'
for i=1:n
xy = randn(5,2);
whos
nframes = size(x, 1);
dmean=zeros(nframes, 1);
%This loop iterates over all the pair of coordinates for current fragment 'i'
for i=1:nframes
xy = [x(i,:)', y(i, :)'];
d = pdist2(xy, xy);
% extract the lower triangle
[ii, jj] = meshgrid(1:5, 1:5);
d1 = d(ii>jj); % d21, d31, ..., d61, d32, .., d62, ... d65
dmean(i) = mean(d1);
end
plot(1:nframes, dmean)
xlabel('frame number');
ylabel('mean distance')
end
end
Moreover, based on the specific usecase the above code can be optimized and nested loops can be avoided.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Introduction to Installation and Licensing finden Sie in Help Center und File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by