商店的公式作为在Matlab阵列的元件

商店的公式作为在Matlab阵列的元件

本文介绍了商店的公式作为在Matlab阵列的元件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(*我不知道在Matlab编程,它只是关于Matlab语言一个一般性的问题。*)

在Excel中,我们可以存储在细胞中的公式。例如,如果 A2 包含公式 = A1 + 10 的再评价A2 收益 30 A1 的值为 20

我的问题是,有没有在Matlab类似的机制?也就是说,我们可以指定一个配方在Matlab中数组的元素,使我们可以重新评估后阵?

编辑1:
继@rayryeng的评论我尽量让一个例子来说明这个概念...其实,这正是持何preadsheet语言如Excel可以做到。

所以我的问题是,是否有允许在Matlab以下机制? (请注意,下面的语法是只是象征)

 >> B = [1 2; B {1,1} +2 4] //存储阵列中的一些价值观和一个公式B =     1 2
     3 4>> B {1,1} = 10 //改变一个单元格的值B =     10 2
     3 4>> EVAL(B)//有一个重新评估命令重新计算所有单元格ANS =     10 2
     13 4


解决方案

希望我理解你想要什么正确的,但得到的答复是确实肯定的。你可以存储在一个单元阵列,每个元素都是一个手柄或匿名函数公式。

也许你的意思是这样的:

 公式= {@(x)x + 10,@sin,@cos,@(X)X / 3};

语法 @ 表示一个函数句柄和(X)表示,这是一个匿名函数与输入变量 X 。第一个单元格元素提供了功能,增加了10到每一个进入它的价值,第二个和第三个参数是句柄 COS ,所以这些行为就像那些三角函数。最后把手把每一个由3进入它的价值。

为了演示,让我们创建一个小阵,然后通过每个公式和他们每个人都适用于小数组:

 >>公式= {@(x)x + 10,@sin,@cos,@(X)X / 3};
>> A = [1 2; 3 4]A =     1 2
     3 4>>公式{1}(A)ANS =    11 12
    13 14>>公式{2}(A)ANS =    0.8415 0.9093
    0.1411 -0.7568>>公式{3}(A)ANS =    0.5403 -0.4161
   -0.9900 -0.6536>>公式{4}(A)ANS =    0.3333 0.6667
    1.0000 1.3333

我们首先创建的公式,然后创建小的2×2矩阵[1 2; 3 4] 。之后,我们访问每个公式的单元格,然后把输入 A 进入功能,我们可以得到你所看到的。


然而,当你开始,开始与功能的脚本实际上声明函数....不要使用这种编程风格的实际应用。它使你的code的可读性。例如,在做罪(A)更可读的公式{2}(A)。人们阅读你的code要记住什么位置数组中对应于您申请输入中的每个元素是什么公式。

(* I don't know programming in Matlab. It is just a general question about Matlab language. *)

In Excel, we can store a formula in a cell. For instance, if A2 contains a formula = A1+10, the re-evaluation of A2 returns 30 when the value of A1 is 20.

My question is, is there a similar mechanism in matlab? That said, can we specify a formula in a element of an array in Matlab, so that we can re-evaluate the array later?

Edit 1:Following the comment of @rayryeng I try to make an example to illustrate the concept... Actually, this is exactly what spreadsheet languages such as Excel can do.

So my question is, is there a mechanism that permits the following in Matlab? (Note that the following syntax is just symbolic)

>> B = [1 2; B{1,1}+2 4] // store some values and a formula in the array

B =

     1     2
     3     4

>> B{1,1} = 10 // change the value of one cell

B =

     10     2
     3      4

>> EVAL(B) // there is a re-evaluation command to re-calculate all the cells

ans =

     10     2
     13     4
解决方案

Hopefully I'm understanding what you want correctly, but the answer is indeed yes. You can store "formulas" in a cell array where each element is a handle or an anonymous function.

Perhaps you mean something like this:

formulae = {@(x) x+10, @sin, @cos, @(x) x / 3};

The syntax @ denotes a function handle and the (x) denote that this is an anonymous function with the input variable x. The first cell element provides a function that adds 10 to every value that goes into it, the second and third parameters are handles to sin and cos, so these act like those trigonometric functions. The last handle divides every value that goes into it by 3.

To demonstrate, let's create a small array, then go through each formula and apply each of them to the small array:

>> formulae = {@(x) x+10, @sin, @cos, @(x) x / 3};
>> A = [1 2; 3 4]

A =

     1     2
     3     4

>> formulae{1}(A)

ans =

    11    12
    13    14

>> formulae{2}(A)

ans =

    0.8415    0.9093
    0.1411   -0.7568

>> formulae{3}(A)

ans =

    0.5403   -0.4161
   -0.9900   -0.6536

>> formulae{4}(A)

ans =

    0.3333    0.6667
    1.0000    1.3333

We first create the formulae, then create a small 2 x 2 matrix of [1 2; 3 4]. After, we access each formula's cell, then put in the input A into the function and we get what you see.


However, when you're starting out, start with actually declaring functions in function scripts.... don't use this kind of style of programming for practical applications. It makes your code less readable. For example, doing sin(A) is much more readable than formula{2}(A). People reading your code have to remember what position in the array corresponds to what formula you are applying to each element in the input.

这篇关于商店的公式作为在Matlab阵列的元件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:58