Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
matlab中的数组到整数。你是怎么做到的_Matlab - Fatal编程技术网

matlab中的数组到整数。你是怎么做到的

matlab中的数组到整数。你是怎么做到的,matlab,Matlab,我有这个,我得到了错误“下标索引必须是实的 正整数或逻辑。“我被告知这是因为我的x和y数组不是整数,但如何使它们成为整数?您的尝试存在一些问题 x=[.4 1 1.4 1.9 2.4] y=[.3 .6 .9 1.2 1.5] for (i=1:10) for (j=1:10) T(x,y)=x.^2+.6.*x.*y.^3-2.*y.^4 end end surf(T') 鉴于: [X, Y] = meshgrid([.4 1 1.4 1.9 2.4], [.

我有这个,我得到了错误“下标索引必须是实的
正整数或逻辑。“我被告知这是因为我的x和y数组不是整数,但如何使它们成为整数?

您的尝试存在一些问题

x=[.4 1 1.4 1.9 2.4]
y=[.3 .6 .9 1.2 1.5]
for (i=1:10)
    for (j=1:10)
        T(x,y)=x.^2+.6.*x.*y.^3-2.*y.^4
    end
end
surf(T')
鉴于:

[X, Y] = meshgrid([.4 1 1.4 1.9 2.4], [.3 .6 .9 1.2 1.5]);
T = X.^2+.6.*X.*Y.^3-2.*Y.^4;
surf(T');
1) 您正在迭代的元素比您实际拥有的要多:
for(i=1:10)
应该是
for(i=1:5)
——但您不应该使用
i
j
作为迭代变量。因此,请使用:

x = [.4 1 1.4 1.9 2.4]
y = [.3 .6 .9 1.2 1.5]
2) 对于变量
T
的索引,您需要使用迭代变量,而不是
x
y
本身

然后循环可能是这样的:

for ii = 1:numel(x)          %// numel(x) = number of elements of x = 5
您可以在

您可能想知道为什么需要转置
T
——这是因为
meshgrid
交换输入。 或者使用
ndgrid

[X, Y] = meshgrid(x, y);
T = X.^2 + .6.*X.*Y.^3 - 2.*Y.^4;
surf(T');
所有三个返回:

这没有太大意义,因为
x轴
y轴
与实际值完全无关。因此你的表面是畸形的。使用
meshgrid
的输出
X
Y
作为
surf
的附加输入:

[X, Y] = ndgrid(x, y);
T = X.^2+.6.*X.*Y.^3-2.*Y.^4;
surf(T);
重复这个数字,我假设您想要:


你想对循环做什么?@user3440208如果其中一个答案帮助你将其标记为“已接受”(左边的复选框),那么系统知道问题已经解决。几乎,假设@user3440208希望第一个索引对应于x(因此
surf(T')
),你需要
[Y,x]=meshgrid([3.6.9 1.2 1.5],[4.1 1.4 1.9 2.4]);
.Meshgrid没有按您可能期望的方式进行索引。我没有意识到在这个问题中他在做
t(x,y)=..
所以我不明白他为什么把t的转置放在末尾..现在它有意义了.谢谢你纠正了错误!我完全忘了把X和Y的值传递给
冲浪
.也许在实际应用中,我会注意到轴有一些语义。。
[X, Y] = ndgrid(x, y);
T = X.^2+.6.*X.*Y.^3-2.*Y.^4;
surf(T);
surf(X,Y,T');