如何实现,不分别指定每个组件(如下所示),或循环索引,如下所示的函数:
function h = f(a)
h(1) = a(2)*a(3)/a(1);
h(2) = a(3)*a(1)/a(2);
h(3) = a(1)*a(2)/a(3);
endfunction
答案 0 :(得分:4)
考虑到在所有情况下表达式都可以重写为a(1)*a(2)*...*a(n)/a(i)^2
,我们获得以下内容:
function h = f(a)
for i=1:length(a)
h(i) = prod(a)/a(i)^2;
endfor
endfunction
另一种解决方案:
function h = f(a)
h = prod(a)./a.^2;
endfunction