I am trying to remove elements from a vector in Matlab by some condition. For example, I want to delete all even numbers.
a = [1 2 3 4 5 6]; for i = 1:numel(a) if (mod(a(i), 2)) == 0 a(i) = []; end end At the same time I receive "Index exceeds matrix dimensions." . It looks like the cycle continues to spin to the original length of the vector, despite the fact that it has decreased. How to deal with it?
But to be completely accurate, I want to write this cycle:
function [centers, rads, metrics] = ... DeleteOverlapCircles(centers, rads, metrics) for i = 1:(length(metrics) - 1) for j = (i + 1):length(metrics) minRad = min([rads(i), rads(j)]); maxRad = max([rads(i), rads(j)]); if pdist2(centers(i), centers(j)) < maxRad + 1 / 2 * minRad metrics(j) = []; centers(j) = []; rads(j) = []; end end end end The question was resolved as follows:
function [centers, rads, metrics] = ... DeleteOverlapCircles(centers, rads, metrics) for i = (length(metrics) - 1):-1:1 for j = (i - 1):-1:1 minRad = min([rads(i), rads(j)]); maxRad = max([rads(i), rads(j)]); if pdist2(centers(i, :), centers(j, :)) ... < maxRad + 1 / 2 * minRad centers(i, :) = []; rads(i) = []; metrics(i, :) = []; break; end end end end But I am not ready to believe that there is no more obvious and beautiful solution.