Amir Hossein F
Amir Hossein F

Reputation: 420

How to remove an element from a 2D vector of length N in MATLAB

Suppose I have a 2D vector of length 5 in MATLAB such as:

A=
[1 2;
 3 4;
 5 6;
 7 8;
 9 10]

and this the location of certain pixels in an image. By checking a condition such as if A(:,1) < 2 && A(:,2) > 9 I want to remove two points [1 2] and [9 10] from A and be left with a new A of length 3:

newA=
[
 3 4;
 5 6;
 7 8]

Upvotes: 1

Views: 55

Answers (2)

Shvet Chakra
Shvet Chakra

Reputation: 1043

A=[1,2;3,4;5,6;7,8;9,10];
disp('Original A');
disp(A);
B=[];
j=1;
for i=1:1:size(A)

    if(A(i,1)<2 || A(i,2)>9)
    else
        B(j,:)=A(i,:);
        j=j+1;
    end

end
A=[];
A=B;
disp('updated A');
disp(A);

output will be

Original A
     1     2
     3     4
     5     6
     7     8
     9    10

updated A
     3     4
     5     6
     7     8

Upvotes: 1

R.Falque
R.Falque

Reputation: 944

I think what you want to do can be done as follow:

A(A(:,1) < 2 | A(:,2) > 9, :) = []

I think you didn't define your condition properly ;)

Upvotes: 4

Related Questions