Filter löschen
Filter löschen

Question regarding matrix with NaN.

1 Ansicht (letzte 30 Tage)
Sebastian Daneli
Sebastian Daneli am 4 Apr. 2022
Beantwortet: Stephen23 am 4 Apr. 2022
I have the following matrix:
X=[9 6 9; 0 2 nan; 3 1 2]
and from this one, I would like the following matrix
new=[3 3 3 2 2 3 3 3; 9 6 9 0 2 3 1 2]
i.e., a new matrix with the number of entries at any given row in matrix X, and a new row omitting the NaN in X.

Akzeptierte Antwort

David Hill
David Hill am 4 Apr. 2022
s=sum(~isnan(X),2);
a=X';
newX=[repelem(s',s');a(~isnan(a))'];

Weitere Antworten (1)

Stephen23
Stephen23 am 4 Apr. 2022
X = [9,6,9;0,2,nan;3,1,2]
X = 3×3
9 6 9 0 2 NaN 3 1 2
tmp = X.';
idx = ~isnan(tmp);
cnt = sum(idx,1);
M = [repelem(cnt,cnt);tmp(idx).']
M = 2×8
3 3 3 2 2 3 3 3 9 6 9 0 2 3 1 2

Kategorien

Mehr zu Linear Algebra finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by