Clear Filters
Clear Filters

How to index all columns but one in a matrix?

70 views (last 30 days)
Hi!
I want to do index/use all columns in a matrix but a single one, so for example:
A=[1 2 3 4; 1 2 3 4; 1 2 3 4] and I 'd like to get at B=[1 2 4; 1 2 4; 1 2 4].
A long way to that would be: B=[A(:,1:2),A(:,4)], but is there anything faster/built in?
Best Peter

Accepted Answer

James Tursa
James Tursa on 2 Aug 2013
B = A;
B(:,3) = [];
  7 Comments
James Tursa
James Tursa on 3 Aug 2018
Because [1 1 0 1] is not a logical vector and you are not accounting for both dimensions. E.g., compare your code to this:
X(:,logical([1 1 0 1]))

Sign in to comment.

More Answers (1)

David Sanchez
David Sanchez on 2 Aug 2013
% no need of a new matrix, remake the old one:
A = exp(A(:,[1:2,4]));

Categories

Find more on Creating and Concatenating Matrices 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!