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
Wan Ji
Wan Ji 2021년 8월 27일
Can you give an accept if it helped you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by