您可以在Matlab中执行延迟集(在Mathematica中为:=)吗? [英] Can you perform a delayed set (:= in Mathematica) in Matlab?

查看:124
本文介绍了您可以在Matlab中执行延迟集(在Mathematica中为:=)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我最近已从Mathematica转换为Matlab,尽管Matlab具有Mathematica的大多数有用功能,但我不知道如何执行与Mathematica的延迟设置操作':='等效的操作,该操作会为变量a赋值.懒惰地获得价值.

So, I've recently converted from Mathematica to Matlab, and while Matlab has most of Mathematica's useful features, I can't figure out how to perform the equivalent of Mathematica's delayed set operation ':=' which assigns a variable a value in a lazy fashion.

例如,在Mathematica中:

For example, in Mathematica:

y = 2;

y = 2;

x:= y;

y = 3;

x

将x的值设置为3,而在Matlab中获得相同行为的唯一方法是:

would give the value of x as 3, whereas the only way I can get this same behavior in Matlab is:

y = 2;

y = 2;

x = @()(y);

x = @()(y);

y = 3;

x()

从技术上回答我的问题,这是一个非常特别的解决方法,需要将x视为函数.

which, while technically answering my question, is a pretty ad hoc work around and requires treating x as a function.

那么Matlab有没有更自然的方法呢?

So is there a more natural way to do this is Matlab?

我的即席解决方案仅在y是句柄类的字段时才有效,为了清楚起见,我在代码中省略了它(应该是someclass.y).最好,我的问题的答案没有此限制,但如果确实有,我仍然会接受.

my ad-hoc solution only works when y is a field of a handle class, I left this out of the code for clarity (it should be someclass.y). Preferably the answer to my question wouldn't have this restriction, but I'd still accept it if it did.

推荐答案

惰性评估主要是在函数式编程语言中使用的MATLAB和MATLAB是基于过程/OOP的.因此,SetDelayed的等效项不存在.正如您已经证明的那样,如果您尝试使用已证明的匿名函数,那么它将无法正常工作.

Lazy evaluation is primarily used in functional programming languages and MATLAB is procedural/OOP based. As such, an equivalent of SetDelayed does not exist. If you try to use anonymous functions as you've demonstrated, it will not work, as Amro has already pointed out.

但是,如果您可以访问符号计算工具箱,则可以尝试使用与:=等效的工具(如果您问我,那是脆弱的等效工具).这是一个示例:

However, if you have access to the symbolic computing toolbox, you can get by with something that could be considered an equivalent of := (a flimsy equivalence, if you ask me). Here's an example:

syms x y z; %#Declare x, y and z as symbolic variables
x=y+2; %#Define some value for x
f=@(x)x.^2; %#Define an anonymous function. 

f(x)

ans =

(y + 2)^2

%#Check with z
f(z)

ans =

z^2   

您可以看到它使用了f的实际定义,并且没有像在数值示例中那样捕获x的定义.您还可以更改x的定义,例如,x=1/yf(x)现在将使用x的当前定义.请注意,f仅仅是函数句柄,并且将使用数字/符号参数.例如,

You can see that it uses the actual definition of f, and does not capture the definition of x as it did in your numerical example. You can also change the definition of x to say, x=1/y and f(x) will now use the present definition of x. Note that f is merely a function handle and will take numerical/symbolic arguments. E.g.,

f(1:5)

ans =

     1     4     9    16    25

:=不相似的部分是,它对表达式中存在的术语应用了 only 定义,并且没有更深入的说明(即,它不评估第一次评估的结果可能会产生不同的变量集).因为MATLAB不是基于规则的语言,所以这不足为奇.为了说明我的观点:

The part where it does not resemble := is that it applies the definitions only for the terms present in the expression and does not go deeper (i.e., it does not evaluate the definitions for the different set of variables that might arise as a result of the first evaluation). This is hardly a surprise as MATLAB is not a rule based language. To illustrate my point:

y=z^3; %#Define y
f(x)

ans = 
(y + 2)^2 %#The definition for y is not used.

而Mathematica会给您(z^3+2)^2.

whereas Mathematica would've given you (z^3+2)^2.

Clear[y, z, x, f]
f[x_] := x^2;
y := z^3; x := y + 2;

f[x]

Out[1]= (2 + z^3)^2


最好是您接受两种语言之间的差异,并尝试坚持每种语言中的惯用语.试图否认它并像其他人那样编程可能会使您的生活陷入困境(例如,从C背景开始并坚定地在Mathematica中编写For循环).


It is best if you embraced the differences in the two languages and tried to stick with what is idiomatic in each. Trying to deny it and program in one like the other could make your life miserable (e.g., starting with a C background and adamantly writing For loops in Mathematica).

这篇关于您可以在Matlab中执行延迟集(在Mathematica中为:=)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆