在Matlab中设置向量的初始类型 [英] Set the initial type of a vector in Matlab
问题描述
我想声明一个空向量,该空向量接受用户定义类型的插入.在以下示例中,node
是我用classdef node ...
I'd like to declare an empty vector that accepts insertion of user-defined types. In the following examples node
is a type I've defined with classdef node ...
Matlab解释器拒绝以下代码,因为空向量会自动初始化为double类型,因此不能在其中插入node
.
The following code is rejected by the Matlab interpreter because the empty vector is automatically initialized as type double, so it can't have a node
inserted into it.
>> a = [];
>> a(1) = node(1,1,1);
The following error occurred converting from node to double:
Conversion to double from node is not possible.
下面的代码被接受,因为矢量是在其中用node
初始化的,因此以后可以插入节点.
The code below is accepted because the vector is initialized with a node
in it, so it can later have nodes inserted.
>> a = [node(1,1,1)];
>> a(1) = node(1,2,1);
但是,我想创建一个 empty 向量,该向量可以将节点插入其中.我可以这样笨拙地做到这一点:
However, I want to create an empty vector that can have nodes inserted into it. I can do it awkwardly like this:
>> a = [node(1,1,1)];
>> a(1) = [];
有什么更好的方法?我正在寻找将空向量的初始类型声明为node
的东西.如果我可以组成语法,它将看起来像:
What's a better way? I'm looking for something that declares the initial type of the empty vector to be node
. If I could make up the syntax, it would look like:
>> a = node[];
但这不是有效的Matlab语法.有什么好方法吗?
But that's not valid Matlab syntax. Is there a good way to do this?
推荐答案
可以创建空对象
A = MyClass.empty;
它适用于您的自己的班级,但也适用于 Matlab的班级,例如
It works with your own class, but also with Matlab's class such as
A = int16.empty;
此方法能够使用此语法创建多维空对象
This method is able to create multi-dimensional empty objects with this syntax
A = MyClass.empty(n,m,0,p,q);
只要一维设置为零.
请参见文档.
这篇关于在Matlab中设置向量的初始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!