Apply logical matrix to dataset
이전 댓글 표시
I have a logical matrix (IdxReturns) with the dimension (589x693) which I would like to apply when calculating the product of my return series (MyReturns). However, when I attempt to apply the logical matrix I get a 9387x1 matrix. How do I go about getting around this? I am using a command that looks like the following:
TempReturn = prod(MyReturns(IdxReturns),2);
Because I am calculating the product function across the rows, I would expect a 589x1 matrix as a result. What am I doing wrong? Do I need to apply the logicals separately?
채택된 답변
추가 답변 (4개)
Fangjun Jiang
2011년 11월 14일
Try this example to see how logical index works.
a=magic(3);
b=logical([1 0 1;0 1 0; 0 0 1]);
c=a(b)
It's picking the element from matrix a according to the logical index b and put the result in a vector.
To make your code work, you need to do the following.
MyReturns(~IdexReturns)=1
TempReturn = prod(MyReturns,2);
It sets the value of the non-selected elements to be 1, because you are doing a prod() operation.
Walter Roberson
2011년 11월 14일
t = MyReturns;
t(IdxReturns) = 1;
TempReturn = prod(t, 2);
댓글 수: 1
Walter Roberson
2011년 11월 14일
It is not clear that your logical matrix corresponds to selecting a mix of rows and columns, so it is not clear to us that a rectangular output would even be possible.
Try
TempReturn = prod( MyReturns(find(any(IdxReturns,2)), find(any(IdxReturns,1))), 2)
If that does what you want then probably using a logical matrix is not the best approach for your needs.
If you really need a logical matrix...
TempReturn = prod( reshape(MyReturns(IdxReturns), sum(IdxReturns,2), sum(IdxReturns,1)), 2);
and if that bombs out with a complaint about reshape changing the number of elements then the implication is that your IdxReturns does not mask out row / column combinations.
Brian
2011년 11월 14일
0 개 추천
댓글 수: 1
Fangjun Jiang
2011년 11월 14일
How do you dump those 93 funds? If no criteria, it's easy to do.
a=rand(593,3);
b=a(1:500,:)
Brian
2011년 11월 15일
댓글 수: 2
Fangjun Jiang
2011년 11월 15일
If IdxReturns is the correct index, why this code won't work? The size of matrix is dependent. There is nothing here that can guarantee the size to be 500, or 589.
MyReturns(~IdexReturns)=1
TempReturn = prod(MyReturns,2);
Brian
2011년 11월 15일
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!