I have a 3D matrix xyposframe
and I would like to delete all rows in that matrix where the first column has a number below 150
or over 326
. I tried the following but this didn't give me the desired result:
indexbelow = xyposframe(:, 1) < 150;
indexabove = xyposframe(:, 1) > 326;
xyposframe(indexbelow, :) = [];
xyposframe(indexabove, :) = [];
How can I delete those rows?
CodePudding user response:
The problem is that both indexbelow
and indexabove
are of size n -by- 1, i.e. column vectors with as much elements as there are rows in xyposframe
. With xyposframe(indexbelow, :) = [];
you remove a number of rows, thus making xyposframe
have less than n
rows, resulting in an error when you try to call xyposframe(indexabove, :) = [];
, since you're attempting to index less than n
elements with a total of n
indices.
Possible solutions are to split the operation into two parts, or combine them in a single logical statement:
% First one, then the other
indexbelow = xyposframe(:, 1) < 150;
xyposframe(indexbelow, :) = [];
indexabove = xyposframe(:, 1) > 326;
xyposframe(indexabove, :) = [];
% Combine using logical AND
indexbelow = xyposframe(:, 1) < 150;
indexabove = xyposframe(:, 1) > 326;
xyposframe(indexbelow & indexabove, :) = [];
% Or put everything in a single indexing line using logical OR
xyposframe(xyposframe(:,1)<150 | xyposframe(:,1) > 326) = [];