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

3 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by