logical Indexing

2 ビュー (過去 30 日間)
Nikhil
Nikhil 2011 年 7 月 22 日
assume a as 3*2 matrix
a = [1 , 1 ; 2 , 2 ; 3 , 3];
a = a(a(:,2) > 1);
what i expect from the above statement is that I get a 2*2 matrix but instead i get 2 *1 matrix
the same thing when i try and define a logical array based on single column of a variable that is
flag = a(:,2) > 1
a = a(flag);
I can get around this but saying [flag;flag]; and a(flag) and then trying to get a back into its original dimensions but i feel stupid doing this, I guess i need to get some basics right.

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 7 月 22 日
idx = a(:,2) > 1;
a = a(idx,:)
with a(idx) - note no ,: - you're saying to take the elements indexed by idx.
with a(idx,:) you're saying to take the rows indexed by idx.
  2 件のコメント
Nikhil
Nikhil 2011 年 7 月 22 日
Thanks man, can I ask what is the logic behind the statement are we asking to do it for all columns when we say ",:" ?
Oleg Komarov
Oleg Komarov 2011 年 7 月 22 日
Take the rows indexed by idx and the all the columns.
Equivalent syntaxes:
- a(idx,1:end)
- a(idx, : )

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by