필터 지우기
필터 지우기

How to fill end of rows of a matrix with NaN values?

조회 수: 9 (최근 30일)
Mr M.
Mr M. 2021년 6월 1일
답변: Walter Roberson 2021년 6월 2일
I have a matrix M, and vector of indices K. I would like to make an element M(k,j) = NaN if j >= K(k). It is possible to do this with a vectorized short methode?
  댓글 수: 2
Bob Thompson
Bob Thompson 2021년 6월 1일
편집: Bob Thompson 2021년 6월 1일
Why not just preallocate the matrix with nan values? Do you have any idea of the max size your matrix could be?
M = nan(max(k),max(j));
Mr M.
Mr M. 2021년 6월 2일
I think, you misunderstood my problem. Lets see an example.
let M be: [1,2,3; 1,2,3; 1,2,3; 1,2,3]; An let K be: [2; 3; 1; NaN or 4 or anithing else]. This means I need the following output:
[1;NaN,NaN; 1,2,NaN; NaN,NaN,NaN; 1,2,3]

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

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 6월 1일
Hi,
here is a simple solution, e.g.:
M = magic(5);
M(:,end)=nan

Walter Roberson
Walter Roberson 2021년 6월 2일
M = [1,2,3; 1,2,3; 1,2,3; 1,2,3]
M = 4×3
1 2 3 1 2 3 1 2 3 1 2 3
K = [2; 3; 1; NaN]
K = 4×1
2 3 1 NaN
M((1:size(M,2)) >= K(:)) = nan
M = 4×3
1 NaN NaN 1 2 NaN NaN NaN NaN 1 2 3

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by