我有一个带有两个整数输入的函数,如下所示:
function f = func(n, m)
a = 2;
b = 1;
f = sqrt((n/a)^2 + (m/b)^2);
end
m和n是整数且大于或等于零。 f的前几个值和它们发生的输入如下所示:
n ----- m ----- f
0 ----- 0 ----- 0
1 ----- 0 ----- 0.5
2 ----- 0 ----- 1
0 ----- 1 ----- 1
1 ----- 1 ----- 1.118
等等。我想得到f的前N个值和它们各自的n和m。在matlab中有一种简单的方法吗?
答案 0 :(得分:2)
meshgrid
和arrayfun
可用于为输入范围生成输出数组
nValues = 0:2
mValues = 0:3
[ii,jj] = meshgrid(mValues,nValues)
output = arrayfun(@func,ii,jj)
可以修改两个值向量以获取所需值的范围
output =
0 0.5000 1.0000 1.5000
1.0000 1.1180 1.4142 1.8028
2.0000 2.0616 2.2361 2.5000
要给出问题中矩阵的结果,可以使用以下内容(感谢@Divakar)
[jj(:),ii(:),arrayfun(@func,jj(:),ii(:))]
ans =
0 0 0
1.0000 0 0.5000
2.0000 0 1.0000
0 1.0000 1.0000
1.0000 1.0000 1.1180
2.0000 1.0000 1.4142
0 2.0000 2.0000
1.0000 2.0000 2.0616
2.0000 2.0000 2.2361
0 3.0000 3.0000
1.0000 3.0000 3.0414
2.0000 3.0000 3.1623
答案 1 :(得分:2)
<强>代码强>
%// Parameters
N = 5
a = 2;
b = 1;
%// Extents of n and m would be from 0 to N-1 to account for all possible
%// minimum values of f results resulting from their use
len1 = N-1
%// Create n and m for maximum possible combinations scenario, but save
%// them as n1 and m1 for now, as the final ones would be chopped versions
%// of them.
[n1,m1] = ndgrid(0:len1,0:len1)
%// Get corresponding f values, but store as f1, for the same chopping reason
f1 = sqrt((n1(:)./a).^2 + (m1(:)./b).^2);
%// Sort f1 so that the smallest N values from it could be choosen and also
%// get the selected row indices based on the sorting as row1
[f1,row1] = sort(f1)
%// Choose n and m based on the sorted indices and also chop off at N.
%// Use these n and m values to finally get f
n = n1(row1(1:N))
m = m1(row1(1:N))
f = f1(1:N)
<强>输出强>
如果N = 5,你会得到 -
n =
0
1
2
0
1
m =
0
0
0
1
1
f =
0
0.5000
1.0000
1.0000
1.1180
如果N = 9,你会得到 -
n =
0
1
2
0
1
2
3
3
4
m =
0
0
0
1
1
1
0
1
0
f =
0
0.5000
1.0000
1.0000
1.1180
1.4142
1.5000
1.8028
2.0000
答案 2 :(得分:1)
像这样的东西(可能效率很低)?
N = 100 % stop
i = 0
n = 0
m = 0
nout = [n]
mout = [m]
fout = [f(n,m)]
while i ~= N
a = f(n+1,m)
b = f(n,m+1)
if (a > b)
m = m + 1
nout = [nout n]
mout = [mout m]
fout = [fout b]
else
n = n + 1
nout = [nout n]
mout = [mout m]
fout = [fout a]
end if
i = i + 1
end while