Combining Logical Indexes

조회 수: 7 (최근 30일)
Brian
Brian 2011년 11월 4일
I have two logical Indexes that I need to reference with my large Matrix of data before performingons. One Index (MyDates) is 1x693 and the other (MyTickers) is 589x1. I need to combine the two Indexes into one large 589x693 logical index. How do I go about oing that?
Thanks, Brian

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 11월 4일
What is the operation, and, or, plus, minus?
bsxfun(@and,MyTickers,MyDates)
  댓글 수: 3
Fangjun Jiang
Fangjun Jiang 2011년 11월 4일
Then @and is what you need!
Sean de Wolski
Sean de Wolski 2011년 11월 4일
Fangjun is correct! times would work, but it's slightly slower since ML has to do a multiplication instead of a logical operation.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Brian
Brian 2011년 11월 4일
Excellent that works great. I used the syntax below.
Idx3 = bsxfun(@and,index2,index1)
So now Idx3 is a 5x5 logical matrix. However, when I try to apply this logical matrix to my 5x5 matrix (MyRtn) in the format MyRtn2 = MyRtn(Idx3) I end up with MyRtn2 being a 9x1 matrix??? Why would that be happening?
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 11월 4일
What shape were you expecting? When you extract arbitrary elements out of a matrix, there is no way for MATLAB to leave "hole" that preserve any original existing shape.
Fangjun Jiang
Fangjun Jiang 2011년 11월 4일
That is expected. If you want to keep the shape of the original matrix, you need fill it with something depending on your need. For example,
a=magic(3);
b=logical([1 1 0;0 1 0;0 1 0]);
a(~b)=nan;

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by