フィルターのクリア

Inter individual distance between fishes

1 回表示 (過去 30 日間)
Shramana
Shramana 2024 年 3 月 22 日
編集済み: Divyanshu 2024 年 4 月 2 日
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 件のコメント
Matt J
Matt J 2024 年 3 月 22 日
Can't you just wrap the code you already have in a loop?
Shramana
Shramana 2024 年 3 月 26 日
I tried but it is not working. This is the code,
% xy = randn(5,2);
x = readmatrix("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1153563/X_fragment1.txt");
y = readmatrix("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1153573/Y_fragment1.txt");
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')

サインインしてコメントする。

回答 (1 件)

Divyanshu
Divyanshu 2024 年 4 月 2 日
編集済み: Divyanshu 2024 年 4 月 2 日
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.

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by