Select specific elements in cell matrix
Show older comments
Hi everyone,
I have a cell with the information of the number of centroids within each image.

I'm running the following code in order to get the distances between each centroid through 12 pictures.
dist = cell(length(prop)-1,1);
for f = 1 : 11 %length(prop)-1
aux1 = [prop{f}.Centroid];
aux2 = [prop{f+1}.Centroid];
cf1 = zeros(length(prop{f}),2);
cf2 = zeros(length(prop{f+1}),2);
cf1(:,1) = aux1(1:2:end);
cf1(:,2) = aux1(2:2:end);
cf2(:,1) = aux2(1:2:end);
cf2(:,2) = aux2(2:2:end);
d = [];
for m = 1 : length(prop{f})
for n = 1 : length(prop{f+1})
aux = abs(cf1(m,:) - cf2(n,:));
if aux(1) < 29.000 && aux(2) < 14.5000
d = [d; aux];
end
end
dist{f} = d;
end
end
My question is how I should I do the loop for just taking colums 6 to 12 instead of doing all? I wanna do two loops separetely. One from colums 1 to 5 and another from 6 to 12.
Thanks in advanced,
Answers (1)
Your aux, cf portion is one confusing piece of code, which I assume could all be replaced by:
cf1 = reshape([prop{f}.Centroid], 2, [])';
cf2 = reshape([prop{f+1}.Centroid], 2, [])';
However, this begs the question: why aren't the centroid already stored as an nx2 matrix?
Your m and n loops are not needed at all, you could obtain the same result with just:
cfdist = reshape(abs(permute(cf1, [1 3 2]) - permute(cf2, [3 1 2])), [], 2);
dist{f} = cfdist(all(cfdist < [29, 14.5], 2), :);
Note that as it is, there's a bug in your code, the dist{f} = d should be after the end of the m loop.
I do not understand your final question.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!