How can I insert NaN value in a matrix?

15 ビュー (過去 30 日間)
Ashfaq Ahmed
Ashfaq Ahmed 2021 年 11 月 30 日
編集済み: dpb 2021 年 11 月 30 日
Suppose I have this matrix
a = [1:6; 7:12; 13:18; 19:24]
a = 4×6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
I want to fill the values for all the even number with NaN. How can I do it? For example, the first row of my matrix should look like:
a(1,:) = 1 NaN 3 NaN 5 NaN

採用された回答

dpb
dpb 2021 年 11 月 30 日
編集済み: dpb 2021 年 11 月 30 日
a(iseven(a))=nan; % iseven() is in Signal Processing TB
w/o the SP TB, it's not much more
a(~mod(a,2))=nan;
These are logical addressing/indexing expressions, one of the most powerful of all MATLAB syntax features. Look for and read section in the documentation on addressing matrices to learn more.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by