避免在MATLAB中矩阵之间被零除 [英] Avoid division by zero between matrices in MATLAB
本文介绍了避免在MATLAB中矩阵之间被零除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用matlab,并且有两个矩阵:
I'm using matlab and I have two matrices :
G =
1 1 1 1
1 1 1 1
和第二个:
m =
4 4 4 4
0 0 0 0
我想要这个结果:
x =
1/4 1/4 1/4 1/4
0 0 0 0
到目前为止,我所做的是:
What I did so far is this :
x = G ./ m
但是它返回:
x =
1/4 1/4 1/4 1/4
NaN NaN NaN NaN
如果除以零,我如何放置默认值"0"来避免除以零?
How can I avoid the divison by zero by placing a default value "0" if there is a division by zero ?
推荐答案
您可以将NaN转换回零:
You can convert the NaNs back to zero:
x = G ./ m;
x(isnan(x))=0; % thanks to comment by @nkjt
或者,如果在矩阵m
中也要保存NaN,则可以执行以下操作:
Or, if you have also NaNs in matrix m
that you want to save, you can do:
x(m==0)=0;
这篇关于避免在MATLAB中矩阵之间被零除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文