我们可以在Matlab中使用混合型矩阵吗?如何? [英] Can we have a mixed-type matrix in Matlab...and how?
问题描述
我正在尝试编写一个以矩阵(特别是矩阵)作为输出的函数,该函数的行显示一个双精度型变量和一个二进制状态".出于没有理由,只是出于好奇,我想知道是否存在一种使行具有不同类型的方法.
I'm trying to write a function with a matrix (specifically a matrix) as the output, the rows of which show a double-type variable and a binary 'status'. For no real reason and just out of curiosity, I wonder if there is a way to have the rows to have different types.
非常感谢
推荐答案
MATLAB提供了两个可行的选项来存储混合数据类型:
MATLAB offers two viable options for storing mixed data types:
您可以将单元格数组视为特殊矩阵,其中每个元素(称为 cell )可以具有不同的类型(和大小).例如:
You can look at a cell array as a special matrix where and each element (called cell) can be of a different type (and size). For instance:
C = {2, 'Hello'}
是一个存储双精度型和字符串的单元格数组.
is a cell array that stores both a double and a string.
结构还可以存储不同数据类型和大小的值,每个都可以一个不同的 field .例如,上面的单元格数组中的信息可以通过以下方式表示为结构:
Structures can also store values of different data types and sizes, each in a different field. For example, the information in the cell array above can be represented as a structure in the following way:
S.count = 2
S.name = 'Hello'
建议:
似乎一个结构看起来更符合您的需求;我想您将有一个名为status
的字段,它存储一个布尔值,而一个名为number
的字段,它存储一个双精度型.对于多个值,您可以在status
中存储一个布尔向量,在number
中存储一个双精度向量.另外,您可以有一个数组结构,每个数组包含一个布尔值和一个双精度值.
Recommendation:
It seems that a struct looks more elegant for your needs; I suppose you'll have a field called status
storing a boolean value and a field called number
storing a double. For multiple values you can store a vector of booleans in status
and a vector of doubles in number
. Alternatively you can have an array of structs holding one boolean and one double each.
这篇关于我们可以在Matlab中使用混合型矩阵吗?如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!