# Trouble Looping and recording distance between nearest centriods

조회 수: 1(최근 30일)
Nichoals Curci 2021년 4월 24일
답변: Tarunbir Gambhir 2021년 4월 27일
Hi all,
I am having trouble looping through this. I have 20 images of rising bubbles against a black background that I am looking to loop through. The images are of blobs and each blob has it's own centroid. (about 50 or so per image). What I am trying to do is record the nearest blob from image to image. I have been able to do this manually from one image to the next but I am having trouble getting this into a for loop. I am sending all of this to an index matrix that looks something like this:
1 1
2 3
3 6
4 5
5 7
with the first number being the original blob in the first image and the second number being the closest blob in the next image (presumably the same blob that is moving upwards).
Below is my code snippet:
for k = 1:20
k = k %original image
j = k+1; % next image
index_matrix = [1:1:length(centroid(k).X)]';
for m = 1:length(centroid(k).X) % starting bubble in the k image
for ii = 1:length(centroid(j).X) %loop over possible bubbles, finding distance
Dist(ii) = sqrt( (centroid(k).X(m)-centroid(j).X(ii))^2 + (centroid(k).Y(m)-centroid(j).Y(ii))^2);
end
[dumb, ind] = min(Dist);
index_matrix(m,j) = ind;
end
end
The error that I am getting is that the Index exceeds the number of array elements .

댓글을 달려면 로그인하십시오.

### 채택된 답변

Tarunbir Gambhir 2021년 4월 27일
The Index exceeding error might be because you are looping "k" variable from 1 to 20, but the "j" variable is looping from 2 to 21. To avoid this, you should loop the variable "k" from 1 to 19.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by