如何将范围分成更小的块?"

时间:2016-12-02 00:55:36

标签: matlab vector

我使用imfindcircles()算法来识别半径在某个范围内的圆圈,例如[20 80]

但是,使用较小范围时算法效率更高,所以

imfindcircles(IMG, [20 51]);
imfindcircles(IMG, [51 80]);

更有效
imfindcircles(IMG, [20 80]);

我的问题是:如何将范围(在这种情况下为[20 80])分解为n"段"我可以输入for循环或其他此类构造来自动执行此操作吗?

期望的输出:

splitFunc( [20 80], 2)
    % outputs [ [20 50] [51 80] ]
splitFunc( [20 80], 3)
    % outputs [ [20 40] [41 60] [61 80] ]

等等。

我看过使用linspace()reshape(),但我认为他们不会做我需要的。

修改:这是我对已接受答案代码的修改,以解决一些边界问题:

minRadius = 20;
maxRadius = 80;
n = 4; % number of chunks to split range into

grid = floor(linspace(minRadius,maxRadius,n+1));
grid(end) = grid(end);

rangeVec = zeros(2,n-1);

% Split the range into n chunks
for i=1:length(grid)-1
    rangeVec(1,i) = grid(i);
    rangeVec(2,i) = grid(i+1);
end

% Now, increment the start point of each range chunk, to prevent overlap
for i=2:length(rangeVec)
    rangeVec(1,i) = rangeVec(1,i) + 1;
end

1 个答案:

答案 0 :(得分:0)

你确实可以使用linspace我认为你需要更加小心,确保你只获得整数并注意边界条件。

minValue = 20;
maxValue = 80;
Chunks = 9;
grid = floor(linspace(minValue,maxValue,Chunks));
grid(end) = grid(end)+1; % fixes the end boundary condition
for id=1:length(space)-1
    [grid(id),grid(id+1)-1]
    % Do your seperation
end