How to find NaN in a matrix and delete it?
Afficher commentaires plus anciens
Hello,
I've got a large matrix and for two columns I need to find the NaNs and to delete them. I need to delete them in the MarketCapY column and the DebtEquityY column.
Below, you find the code that is already written.
for i=1:28
EquityY= equity (:,i);
TotalDebtY= TotalDebt (:,i);
MarketCapY= MarketCap (:,12*i+1:12*(i+1)+1);
TotalReturnY = TotalReturn (:,12*i+1:12*(i+1)+1);
DebtEquityY= TotalDebtY./EquityY;
DataY= [DebtEquityY MarketCapY TotalReturnY];
[row,col] = find(isnan(MarketCapY));
out = DataY(any(~isnan(MarketCapY),2),:)
[row, col] = find(isnan(DebtEquityY));
out = DataY(any(~isnan(DebtEquityY),2),:);
You can see that the first two columns of DataY must be eliminated. I already tried something, but I don't know if it's correct.
3 commentaires
Image Analyst
le 26 Déc 2018
Can you attach your data (equity, TotalDebt, MarketCap, TotalReturn) in a .mat file so we can visualize what you need.
Keep in mind that a matrix must remain rectangular. If there are Nans scattered around the matrix, you cannot delete them. You can only
- set the nan value to some other valid value
- remove the whole row with the nan in it, or
- remove the whole column with the nan in it.
Sounds like you're wanting option 3 but seeing the matrices would help.
Alyssa Gailliaert
le 26 Déc 2018
Image Analyst
le 26 Déc 2018
That wouldn't work for a matrix, but would for a vector. If DebtEquityY, MarketCapY, and TotalReturnY are all vectors, and they all have nan's in the same position, that would work. If they have nan's in different locations then you'll have to use any() on DataY matrix, like Star Strider showed below. Maybe you could at least vote for the answers below or accept the best one to give them reputation points.
Réponse acceptée
Plus de réponses (1)
madhan ravi
le 26 Déc 2018
a=[1 2 3 NaN]; %an example
a(isnan(a))=[]
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!