Trouble Looping and recording distance between nearest centriods

1 view (last 30 days)
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 .

Accepted Answer

Tarunbir Gambhir
Tarunbir Gambhir on 27 Apr 2021
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.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!