如何为使用循环
问题描述:
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
AA矩阵存储regionprops的结果,当我显示“质心”外,这些for循环只显示最后一次迭代值,我怎样才能使重心通过附加其中一个保持所有迭代结果一。如何为使用循环
实施例:
itration-1: 4,2
itration-2: 6,4
itration-3: 1,3.2
- 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