递归地划分一个平方域-Matlab

递归地划分一个平方域-Matlab,matlab,recursion,iteration,wireless,Matlab,Recursion,Iteration,Wireless,我正在用matlab进行无线传感器网络的仿真 我有一个200*200的区域,其中随机绘制了100个传感器节点。每个节点都有一个关联的负载值。我必须在这片土地上设置充电站。我试图递归地划分这个平方,只要我没有找到一个小的子平方,我只能放置一个充电站。下面是我编写的代码,用于递归划分正方形,并计算可以放置在子正方形中的站点数: %Inputs to the function %numstations - No. of stations to be placed = 10 %b

我正在用matlab进行无线传感器网络的仿真

我有一个200*200的区域,其中随机绘制了100个传感器节点。每个节点都有一个关联的负载值。我必须在这片土地上设置充电站。我试图递归地划分这个平方,只要我没有找到一个小的子平方,我只能放置一个充电站。下面是我编写的代码,用于递归划分正方形,并计算可以放置在子正方形中的站点数:

    %Inputs to the function
    %numstations - No. of stations to be placed = 10
    %boundCoords - A 2*2 matrix with min and max coordinates of square . e.g [0 0;200 200]
    % sensors - A 100*3 matrix for nodes with 1st column as randomly generated 100 x-coordinates, 
    %second column as randomly generated 100 y-coordinates, 
    %third column as corresponding load of each node (can be random)


function stationPoss = deploy(numStations, boundCoords)
global sensors;
centerCoord = mean(boundCoords, 1);
numSensors = size(sensors, 1);
sumQuadLoad = zeros(1, 4);
for i = 1:numSensors
    if sensors(i, 1) < boundCoords(2, 1) && sensors(i, 2) < boundCoords(2, 2)...
            && sensors(i, 1) > boundCoords(1, 1) && sensors(i, 2) > boundCoords(1, 2)
        isIn34Quads = sensors(i, 1) > centerCoord(1); % N
        isIn24Quads = sensors(i, 2) > centerCoord(2);
        biQuadIndex = [isIn34Quads, isIn24Quads];
        quadIndex = bi2de(biQuadIndex) + 1;
        sumQuadLoad(quadIndex) = sumQuadLoad(quadIndex) + sensors(i, 3);
    end
end
if numStations == 1
    [maxQuadLoad, quad] = max(sumQuadLoad); %#ok<ASGLU>
    delta = (centerCoord - boundCoords(1, :)) .* de2bi(quad - 1);
    assoQuadCoords = [boundCoords(1, :); centerCoord] + repmat(delta, 2, 1);
    stationPoss = mean(assoQuadCoords, 1);
else
    sumLoad = sum(sumQuadLoad);
    quadNumStations = zeros(1, 4);
    for i = 1:3
        if sumQuadLoad(i) == 0
            quadNumStations(i) = 0;
        else
            quadNumStations(i) = floor(numStations * sumQuadLoad(i) / sumLoad);
        end
    end
    quadNumStations(4) = numStations - sum(quadNumStations);
    stationPoss = zeros(numStations, 2);
    for i = 1:4
        delta = (centerCoord - boundCoords(1, :)) .* de2bi(i - 1);
        newBoundCoords = [boundCoords(1, :); centerCoord] + repmat(delta, 2, 1);
        if quadNumStations(i) ~= 0
            indexRange = sum(quadNumStations(1:i-1)) + (1:quadNumStations(i));
            stationPoss(indexRange, :) = deploy(quadNumStations(i), newBoundCoords);
        end
    end
end
问题是,当尝试在numStations=2的情况下运行此代码时,它工作正常,而在numStations=3的情况下,它有时会崩溃。对于numStation>3,它几乎总是崩溃。 我试图想出一种非递归的方法来编写这个函数,但没有成功


请任何人帮助我解决崩溃问题,或者为上述函数编写非递归解决方案。我已经尝试增加递归限制。

什么是崩溃?有例外吗?我的意思是它停止工作了。Windows说:matlab.exe已停止工作您的系统是否内存不足?我想是的。这就是为什么它可能会崩溃。是否有任何方法来验证它。对不起,我迟了答复