Understanding why I can't use index on the result of the size function on a matrix in matlab

后端 未结 1 1904
日久生厌
日久生厌 2021-01-26 06:57

When trying:

a = [ 1 1 ; 1 1 ];
size(a)(1)

I get

Error: ()-indexing must appear last in an index
expression

M

相关标签:
1条回答
  • 2021-01-26 07:12

    Because you can't index the result of a function like that in MATLAB without creating a temporary.

    temp = size(a);
    temp(1)
    

    will work. There are often other ways of getting what you want, however. In your example, you can make use of the dim argument in the function size(X,dim):

    size(a,1)
    

    That will get you the size of the first dimension directly, avoiding the need to create a temporary variable.

    0 讨论(0)
提交回复
热议问题