Remove row from matrix that doesn't meet if condition

8 views (last 30 days)
f=@(x) 2-2*x(1)-x(2) %%Check admissibility
for j=1:size(sol,1);
z=sol(j,1:2)
if f(z)>=-1
fprintf('Admissible')
check(j)=0;
else fprintf('Not admissable')
check(j)=1;
end
end
for j=1:size(sol,1) %%Remove non admissible rows from solutions
if check(j)==1
sol(j,:)=[];
end
end
Hi, I am trying to check if the variable value stored in the first two columns of the matrix sol =
[ 0, 0, 0, 0, 0]
[ 1, 0, -1, 0, 0]
[ 0, 2, 4, 0, -10]
[ 6, -2, 0, 10, 0]
[ 0, 4, 0, -8, -12]
[ -2, 6, -16, -30, 0]
satisfy the inequality on f, and, if not, delete the relative row.
I've written the code above, but the problem is that when one row is canceled the dimensionality of the matrix is altered and everything goes awry (unless the row to be deleted is the last one).
How can I fix this?

Accepted Answer

Jan
Jan on 14 May 2013
If you delete the 1st column of sol, deleting the 2nd requires:
sol(1, :) = []
% not sol(2, :) = [] !!!
because the new 1st row is the old 2nd one already.
Better omit the loop:
sol(check == 1, :) = [];

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!