简体   繁体   English

在MATLAB中迭代函数

[英]Iterating a function in MATLAB

Incredibly simple question, but I think I'm unable to come up with the correct terminology to google search it. 一个非常简单的问题,但是我想我无法用正确的术语来谷歌搜索它。

If I have a snippet of code that relies on three independent variables: 如果我有依赖于三个独立变量的代码片段:

code(x,y,z)

That produces two values, ie: 产生两个值,即:

output1, output2

How do I go about iterating like so (pseudocode): 我该如何进行迭代(伪代码):

for x

    for y

        for z

            code(x,y,z)

        end

    end

end

And have data I can parse to generate 3D graphs such as 并具有我可以解析的数据以生成3D图形,例如

surf(x,y,output1)

A naive solution I came up with was just to create a bin of n length and then iterating one variable n times to come up with a 2D graph, ie: 我想出的一个简单的解决方案是创建一个n个长度的bin,然后迭代一个变量n次以得出2D图形,即:

x_axis = zeros(1,25)

for m = 1:25

    xm = x + 1
    x_axis(m) = xm

    code(x,y,z)

Even a referral to some documentation would be extremely helpful. 甚至引用一些文档也将非常有帮助。

Thanks! 谢谢!

Brute force approach: 蛮力方法:

for x=[1:50]
    for y=[1:50]
        for z=[1:50]
            result(y,x,z)=code(x,y,z);
        end
    end
end

More paradigmatic approach (in MATLAB) is to meshgrid it, and pump those in. (在MATLAB中)更典型的方法是对其进行meshgrid ,然后将其meshgrid

[XX,YY,ZZ]=meshgrid([1:50],[1:50],[1:50]);
result=code(XX,YY,ZZ);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM