在 MATLAB 中向量化函数求值 [英] Vectorize function evaluation in MATLAB
本文介绍了在 MATLAB 中向量化函数求值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下功能,
function Vectorize()
a = randn(1,5)
b = randn(1,5)
c = zeros(1,5)
for i=1:5
c(i) = (a(i) - b(i))/(1+a(i)/2+b(i)/3)
end
我想对上述函数求值进行向量化并替换 for
循环.
I want to vectorize the above function evaluation and replace the for
loop.
我可以做 c = a -b
,找到两个行向量之间的差异.我不知道如何处理除法 a/2 和 b/2.
I could do c = a -b
, that finds the difference between two row vectors.I am not sure how to handle the division a/2 and b/2.
有人可以帮忙吗?
推荐答案
您需要元素明智的除法操作 ./
You need the element wise division operation ./
c = (a - b)./(1+a/2+b/3)
如果将向量除以标量,这不是必需的,但是在将数组除以数组的情况下,您必须使用 ./
.有关其他元素,请参阅此处明智的运营商.
If you divide a vector by a scalar, this is not required, but where you divide an array by an array you will have to use ./
in your case. See here for the other element wise operators.
这篇关于在 MATLAB 中向量化函数求值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文