如何使用 for 循环将 regionprops 的结果存储为矩阵

How to store result of regionprops as a a matrix using for loop

for i=1:length(blocks)
    for j=1:length(blocks)
        temp = blocks{i,j};
        s = regionprops(temp, 'Centroid');
        centroids= cat(1,s.Centroid);  
    end
end

当我在这些 for 循环之外显示 "centroids" 时,它只显示最后一次迭代值,我怎样才能让质心通过一个接一个地附加它们来保持所有迭代结果。

示例:

itration-1: 4, 2

itration-2: 6, 4

itration-3: 1, 3.2

itration-4: 2, 2.5

所以

centroids = 
[4 2;
6 4;
1 3.2;
2 2.5];

但我得到的结果是只有最后一次迭代值 2,2.5;我怎样才能保留所有迭代的所有值

您可以将 centroids 连接到数组的末尾,如下所示:

centroids_arr = []; %Initialize centroids array to empty array.

for i=1:length(blocks)
    for j=1:length(blocks)
        temp = blocks{i,j};
        s = regionprops(temp, 'Centroid');
        centroids= cat(1,s.Centroid);  

        %Concatenate last value of centroids to the end of centroids_arr array (insert as new row to the bottom). 
        centroids_arr = [centroids_arr; centroids];
    end
end