How to index all columns but one in a matrix?

34 ビュー (過去 30 日間)
Peter
Peter 2013 年 8 月 2 日
コメント済み: James Tursa 2018 年 8 月 3 日
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

採用された回答

James Tursa
James Tursa 2013 年 8 月 2 日
B = A;
B(:,3) = [];
  7 件のコメント
Brando Miranda
Brando Miranda 2018 年 8 月 3 日
why does X([1 1 0 1]) not work?
James Tursa
James Tursa 2018 年 8 月 3 日
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]))

サインインしてコメントする。

その他の回答 (1 件)

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by