我想问一下是否有任何方法可以实现一个可以使用点分隔符生成ID的程序"." 例如:
a1.b2.c3
Run Code Online (Sandbox Code Playgroud)
请注意,我不想将点作为一个字符处理,它应该像一个分隔符.
如果你在你的名字和你父亲的名字以及你祖父的名字之间加一个点,就像这样:
John.Paul.Hit
Run Code Online (Sandbox Code Playgroud)
小智 6
正如所指出的,你已经可以做到这一点.但是,你应该认识到效率会降低
A = 3;
B.C.D.E = 3;
whos A B
Name Size Bytes Class Attributes
A 1x1 8 double
B 1x1 536 struct
Run Code Online (Sandbox Code Playgroud)
看到B占用了比A更多的存储空间.
另外,你需要认识到AB和AC在MATLAB中不是不同的对象,而是同一结构的一部分,A.事实上,如果我现在尝试创建AB,它会感到沮丧,因为A已经存在为double.
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
Run Code Online (Sandbox Code Playgroud)
原始变量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
Run Code Online (Sandbox Code Playgroud)
看到向A添加2是如此之快,以至于timeit无法测量它.但是为BCD添加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
Run Code Online (Sandbox Code Playgroud)
对于这些变量中的任何一个,计算都同样快.
| 归档时间: |
|
| 查看次数: |
1795 次 |
| 最近记录: |