How to replace elements different than 0 and different than NaN to 1?

I have this huge matrix with a lot of elements: (i) equal to zero; (ii) number different than zero; (iii) NaN values ...
I need to replace all the the NUMBERS different than 0 to 1.
How do I do it not replacing the NaN values?

 採用された回答

Mohammad Monfared
Mohammad Monfared 2013 年 10 月 24 日

0 投票

Let the matrix name, A, so this is done by:
A(xor((A~=0),isnan(A))) = 1 ;

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 24 日
編集済み: Azzi Abdelmalek 2013 年 10 月 24 日

1 投票

A(A~=0)=1
%or maybe you want
idx=isnan(A) | A==0
A(~idx)=1

3 件のコメント

cfjunior
cfjunior 2013 年 10 月 24 日
I tried this ... but it replaces NaN values too, and I don't wan't it...
I just want to replace the numbers different than zero...
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 24 日
編集済み: Azzi Abdelmalek 2013 年 10 月 24 日
Ok, then just run the second code
idx=isnan(A) | A==0
A(~idx)=1
cfjunior
cfjunior 2013 年 10 月 24 日
tks!

サインインしてコメントする。

カテゴリ

質問済み:

2013 年 10 月 24 日

コメント済み:

2013 年 10 月 24 日

Community Treasure Hunt

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

Start Hunting!

Translated by