Apply logical matrix to dataset
Mostrar comentarios más antiguos
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?
Respuesta aceptada
Más respuestas (4)
Fangjun Jiang
el 14 de Nov. de 2011
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
el 14 de Nov. de 2011
t = MyReturns;
t(IdxReturns) = 1;
TempReturn = prod(t, 2);
1 comentario
Walter Roberson
el 14 de Nov. de 2011
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
el 14 de Nov. de 2011
0 votos
1 comentario
Fangjun Jiang
el 14 de Nov. de 2011
How do you dump those 93 funds? If no criteria, it's easy to do.
a=rand(593,3);
b=a(1:500,:)
Brian
el 15 de Nov. de 2011
2 comentarios
Fangjun Jiang
el 15 de Nov. de 2011
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
el 15 de Nov. de 2011
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!