Logical indexing with matrix issue
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Yu Jiang
2014년 8월 27일
편집: Yu Jiang
2014년 8월 27일
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
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!