Squeezing NaN's out of a square matrix to make a smaller square matrix

2 ビュー (過去 30 日間)
Shannon
Shannon 2021 年 8 月 27 日
コメント済み: Wan Ji 2021 年 8 月 27 日
I have a matrix that looks like this:
M =[NaN 1 1 1 1
2 NaN 2 2 2
3 3 NaN 3 3
4 4 4 NaN 4
5 5 5 5 NaN];
and I want to remove the NaN's and retain the matrix shape so that it looks like this:
M =[2 1 1 1 1
3 3 2 2 2
4 4 4 3 3
5 5 5 5 4];
I can't figure out how to code that though. Any suggestions?

採用された回答

Wan Ji
Wan Ji 2021 年 8 月 27 日
[p,q] = size(M);
M(isnan(M))=[];
M = reshape(M,p-1,q)
Then
M =
2 1 1 1 1
3 3 2 2 2
4 4 4 3 3
5 5 5 5 4
  3 件のコメント
Shannon
Shannon 2021 年 8 月 27 日
That does it - thanks!
Wan Ji
Wan Ji 2021 年 8 月 27 日
Can you give an accept if it helped you

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by