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!