MATLAB: How to find NaN in a matrix and delete it

isnannan

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.

Best Answer

You have the logical negation ‘~’ in the incorrect position.
Example —
A = rand(4);
A(2,3) = NaN;
A(3,4) = NaN
Q1 = ~(any(isnan(A),1))
Q2 = ~(any(isnan(A),2))
Out1 = A(:,~any(isnan(A),1)) % Select Columns
Out2 = A(~any(isnan(A),2),:) % Select Rows
You also need to be certain that you are selecting columns or rows.