我们可以在matlab程序中使用“dot”吗?

时间:2012-10-22 09:12:38

标签: matlab

我想询问是否有任何方法可以实现一个可以用点分隔符生成ID的程序“。”例如:

a1.b2.c3 

请注意,我不想将点作为字符处理,它应该像分隔符一样。

如果你在你的名字和你父亲的名字以及你祖父的名字之间加一个点,就像这样:

John.Paul.Hit

3 个答案:

答案 0 :(得分:6)

正如所指出的,你已经可以这样做了。但是,你应该认识到效率会降低

A = 3;
B.C.D.E = 3;

whos A B
  Name      Size            Bytes  Class     Attributes

  A         1x1                 8  double              
  B         1x1               536  struct              

看到B占用了比A更多的存储空间。

另外,你需要认识到AB和AC在MATLAB中不是不同的对象,而是同一结构的一部分,A。事实上,如果我现在尝试创建AB,它会感到沮丧,因为A已经存在一双。

A.B = 4
Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures
Displays Warning, for details. 
A = 
    B: 4

原始变量A不再存在。

还有时间问题。结构效率会降低。

timeit(@() A+2)
Warning: The measured time for F may be inaccurate because it is close to the estimated time-measurement overhead (3.8e-07 seconds).  Try measuring
something that takes longer. 

> In timeit at 132 
ans =
    9.821e-07

timeit(@() B.C.D+2)
ans =
   3.6342e-05

看到向A添加2是如此之快,以至于timeit无法测量它。但是在B.C.D中增加2个就需要大约30倍的时间。

所以,最后,你可以用结构做你想做的事情,但是有充分的理由不这样做,除非你对点非常有效。替代分隔符在我所展示的方面效果更好。

A = 3;
A_B_C_D = 3;
whos A*
  Name         Size            Bytes  Class     Attributes

  A            1x1                 8  double              
  A_B_C_D      1x1                 8  double              

这些变量的计算速度同样快。

答案 1 :(得分:2)

Matlab已经将点用作id中的分隔符,特别是在结构及其字段的id中。例如,执行

a.b = 3

创建一个名为a的结构,其中一个名为b的字段本身具有值3。阅读有关structures和函数struct

主题的文档

答案 2 :(得分:0)

不是你想要的方式。如上面的答案所述,点在ML语法中具有特定含义,不能用作标识符本身的一部分。

相关问题