Inter individual distance between fishes
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
Divyanshu
il 2 Apr 2024
Modificato: Divyanshu
il 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
file1 = sprintf("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1153563/X_fragment%d.txt",i);
file2 = sprintf("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1153563/Y_fragment%d.txt",i);
%your piece of code
xy = randn(5,2);
x = readmatrix(file1);
y = readmatrix(file2);
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 Commenti
Vedere anche
Categorie
Scopri di più su Get Started with MATLAB in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!