Arrays 为什么我的Matlab函数不接受数组?
我有以下功能:Arrays 为什么我的Matlab函数不接受数组?,arrays,matlab,parameters,parameter-passing,Arrays,Matlab,Parameters,Parameter Passing,我有以下功能: function [ res ] = F( n ) t = 1.5; res = 0; if n <= 0 return; end for i = 0:n-1 res = res + power(-1,i)*power(t,2*i+1)/((2*i+1)*factorial(i)); end end 出于某种原因,它拒绝对整个数组执行操作,只给我第一个成员的输出。 为什么呢 编辑:如果我改变
function [ res ] = F( n )
t = 1.5;
res = 0;
if n <= 0
return;
end
for i = 0:n-1
res = res + power(-1,i)*power(t,2*i+1)/((2*i+1)*factorial(i));
end
end
出于某种原因,它拒绝对整个数组执行操作,只给我第一个成员的输出。
为什么呢
编辑:如果我改变
res = 0;
初
res = 0 + n;
res = res - n;
它确实适用于整个阵列 问题在于res不是数组。您可以这样做:
function res = F(n)
t = 1.5;
m = length(n);
res = zeros(m, 1);
for j = 1 : m
for i = 0 : n(j) - 1
res(j) = res(j) + power(-1, i) * power(t, 2 * i + 1) / ((2 * i + 1) * factorial(i));
end;
end;
end;
示例向量输入的结果如下:
>> F([2,3,4])
ans =
0.375000000000000
1.134375000000000
0.727566964285714
>> F([2,3,4])
ans =
0.375000000000000
1.134375000000000
0.727566964285714