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?
0 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!