出于某种原因,当我在matlab中尝试size
函数时,我无法确定数据类型,也无法按照我想要的方式使用它。
data = [[3,4,56,1,2],[3,1,3,45,2]];
然后,我想得到第二个维度:
size(data)(2)
抛出错误:
>> size(data)(2)
Error: ()-indexing must appear last in an index expression.
然后我试试这个:
>> n = size(data)
n =
1 10
>> n(2)
ans =
10
这没问题。吹了我的脑海。为什么?以及size
返回什么类型的数据类型?
答案 0 :(得分:2)
试试size(data,2)
。这非常基础。
答案 1 :(得分:1)
正如您的错误所说,MATLAB中不允许这样做。
要在多维数组(矩阵)上使用size函数,请使用size(data, 1)
获取行数,使用size(data, 2)
获取列数。
这是MATLAB上help size
的(第一位)结果。
>>help size
'size' is a built-in function from the file libinterp/corefcn/data.cc
-- Built-in Function: size (A)
-- Built-in Function: size (A, DIM)
Return the number of rows and columns of A.
With one input argument and one output argument, the result is
returned in a row vector. If there are multiple output arguments,
the number of rows is assigned to the first, and the number of
columns to the second, etc.
同样,如果您有一个矩阵A = [1, 2, 3; 4, 5, 6]
,请使用相同的方法来访问这些元素。例如,A(1,1) = 1
(非A(1)(1))和A(2,2) = 5
。
干杯!
修改强>
另外,你的意思是data = [[3,4,56,1,2];[3,1,3,45,2]];
(注意分隔行的半色)?因为data = [[3,4,56,1,2],[3,1,3,45,2]];
会返回一个长10维向量而不是5x2矩阵。