How to fill end of rows of a matrix with NaN values?
16 ビュー (過去 30 日間)
古いコメントを表示
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
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));
回答 (2 件)
Sulaymon Eshkabilov
2021 年 6 月 1 日
Hi,
here is a simple solution, e.g.:
M = magic(5);
M(:,end)=nan
0 件のコメント
Walter Roberson
2021 年 6 月 2 日
M = [1,2,3; 1,2,3; 1,2,3; 1,2,3]
K = [2; 3; 1; NaN]
M((1:size(M,2)) >= K(:)) = nan
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!