필터 지우기
필터 지우기

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 CenterFile Exchange에서 Introduction 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