What's the difference between {} and [] in MATLAB?

前端 未结 5 471
清酒与你
清酒与你 2021-01-03 03:57
>> A={1 2;2 3}

A = 

    [1]    [2]
    [2]    [3]
>> A=[1 2;2 3]

A =

     1     2
     2     3

It seems to me they are essentially

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-03 04:14

    [] is an array-related operator. An array can be of any type - array of numbers, char array (string), struct array or cell array. All elements in an array must be of the same type!

    Example: [1,2,3,4]

    {} is a type. Imagine you want to put items of different type into an array - a number and a string. This is possible with a trick - first put each item into a container {} and then make an array with these containers - cell array.

    Example: [{1},{'Hallo'}] with shorthand notation {1, 'Hallo'}

    It is unnecessary to put objects of the same type (doubles) into a cell array like in your example.

提交回复
热议问题