Borrar filtros
Borrar filtros

How to index all columns but one in a matrix?

70 visualizaciones (últimos 30 días)
Peter
Peter el 2 de Ag. de 2013
Comentada: James Tursa el 3 de Ag. de 2018
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

Respuesta aceptada

James Tursa
James Tursa el 2 de Ag. de 2013
B = A;
B(:,3) = [];
  7 comentarios
Brando Miranda
Brando Miranda el 3 de Ag. de 2018
why does X([1 1 0 1]) not work?
James Tursa
James Tursa el 3 de Ag. de 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]))

Iniciar sesión para comentar.

Más respuestas (1)

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

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by