Logical indexing with matrix issue

I ran into this issue writing a program using logical indexing. Suppose you have
A=ones(32,10);
M=zeros(32,10);
N=ones(1,10);
for i=1:32;
M(i,:)=N;
end
M=logical(M);
C=A(M);
Why is C a 320x1? Shouldn't it be equal to A since the logical is all 1's? Thanks, Charles

 採用された回答

Yu Jiang
Yu Jiang 2014 年 8 月 27 日
編集済み: Yu Jiang 2014 年 8 月 27 日

0 投票

This is the intended behavior of Logical indexing. For example,
A = [1 2; 3 4];
Then, A(logical([1,1;1,1])) will return A(:), which is equal to
[1;
3;
2;
4].
And A(logical([1,0;0,1])) will return
[1;
4]
You can find more examples via the following link

3 件のコメント

Senaasa
Senaasa 2014 年 8 月 27 日
Why does
A=[1 2 3 4 5]
B=A>2
C=A(B)
return C=[3 4 5] and not C=[3;4;5]?
Yu Jiang
Yu Jiang 2014 年 8 月 27 日
Senaasa
You are right. It seems for row vectors, the results are kept as rows. However, if A is a matrix (with more than one row), the result will always be a column vector.
Senaasa
Senaasa 2014 年 8 月 27 日
Thanks for your quick replies. I guess I've only ever used 1 row when using logicals, so I never noticed this issue.
Thanks for your help

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2014 年 8 月 27 日

コメント済み:

2014 年 8 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by