MATLAB Answers

How to exclude more than 1 columns from a matrix?

304 views (last 30 days)
Isti
Isti on 27 Apr 2012
I have a matrix like this: Data = [2,1,4,6,2;9,4,6,1,2;5,3,2,8,3;7,2,1,9,3;7,1,8,2,4]
2 1 4 6 2
9 4 6 1 2
5 3 2 8 3
7 2 1 9 3
7 1 8 2 4
then i want to exclude the column 3 and 5 from my new matrix. So it will be like this:
2 1 6
9 4 1
5 3 8
7 2 9
7 1 2
what to do? thanks before :')

  0 Comments

Sign in to comment.

Accepted Answer

Richard
Richard on 27 Apr 2012
simple way would be:
Data(:,3) = []; Data(:,5) = [];

  3 Comments

Jan
Jan on 27 Apr 2012
This will fail: If the 3rd column is removed, there is no 5th column anymore!
Better: Data(:, [3, 5]) = [];
Richard
Richard on 27 Apr 2012
then you would remove one column first and then change the value accordingly, only a simple solution. Although I agree that Data(:, [3, 5]) = []; is better.
Isti
Isti on 28 Apr 2012
thanks Jan. actuallu when I used the solution above, there's a time the error about exceeded matrix is out.
so, solution from andrei below is same with you. and it works :)

Sign in to comment.

More Answers (1)

Andrei Bobrov
Andrei Bobrov on 27 Apr 2012
Data = [2,1,4,6,2;9,4,6,1,2;5,3,2,8,3;7,2,1,9,3;7,1,8,2,4]
out = Data(:,[1 2 5])

  4 Comments

Show 1 older comment
Andrei Bobrov
Andrei Bobrov on 27 Apr 2012
out = Data(:,setdiff(1:size(Data,2),[3 5]))

Sign in to comment.

Sign in to answer this question.