Access elements of a Vector without a few Indices?

5 ビュー (過去 30 日間)
Pankaj
Pankaj 2015 年 4 月 26 日
コメント済み: Pankaj 2015 年 4 月 26 日
Say suppose I have a vector
X = [4 5 1 7 3 9 2 4]
and I want to access X but without indices: 1, 4 and 6. I tried
X(~[1, 4, 6])
But that's not working. Any suggestions.
Thanks
PS: I don't want to modify the vector.

採用された回答

Stephen23
Stephen23 2015 年 4 月 26 日
編集済み: Stephen23 2015 年 4 月 26 日
You could use setxor to generate the indices:
>> X = [4 5 1 7 3 9 2 4];
>> X(setxor(1:numel(X),[1,4,6]))
ans =
5 1 3 2 4
Or alternatively you could use create a logical array and use logical indexing. Even though it takes more lines it may be faster than the first solution:
>> Y = true(size(X));
>> Y([1,4,6]) = false;
>> X(Y)
ans =
5 1 3 2 4
  1 件のコメント
Pankaj
Pankaj 2015 年 4 月 26 日
Thanks Stephen

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

その他の回答 (0 件)

カテゴリ

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