How to make a log operation for a matrix excluding the '-inf' values?

Hello everyone!
I want to do it a log operation in a matrix and I developed the following loop:
for ii = 1: size (IDHMA,1)
for kk = size (IDHMA,2)
IDHMAlog (ii,kk) = log(IDHMA(ii,kk))
if IDHMA(ii,kk) == 0
IDHMAlog(ii,kk) = NaN %exclude the"-Inf" values
else
IDHMAlog (ii,kk) = log(IDHMA(ii,kk))
end
end
end
But the result is giving me a matrix in the same dimension but with zeros and the the log operation result only in the last column. Does anyone know how to fix it ?
Thanks

 採用された回答

Tommy
Tommy 2020 年 6 月 2 日

1 投票

k should span from 1 to size(IDHMA,2), similar to ii:
for ii = 1: size (IDHMA,1)
for kk = 1 : size (IDHMA,2)
IDHMAlog (ii,kk) = log(IDHMA(ii,kk))
if IDHMA(ii,kk) == 0
IDHMAlog(ii,kk) = NaN %exclude the"-Inf" values
else
IDHMAlog (ii,kk) = log(IDHMA(ii,kk))
end
end
end
I believe you could avoid the loops if you'd like:
IDHMAlog = log(IDHMA);
IDHMAlog(IDHMA==0) = NaN;

1 件のコメント

Julia Moore
Julia Moore 2020 年 6 月 2 日
Yeaah! That worked. Thank you so much!!

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

その他の回答 (0 件)

カテゴリ

質問済み:

2020 年 6 月 2 日

コメント済み:

2020 年 6 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by