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

前端 未结 5 426
清酒与你
清酒与你 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:00

    If you relate it to object oriented programming, cells {} are like objects and [] is for arrays

    0 讨论(0)
  • 2021-01-03 04:02

    {}'s are for cells. []'s are for arrays/matrices.

    0 讨论(0)
  • 2021-01-03 04:07

    No. They are not at all the same thing. The only aspect that is the same is the resulting shape.

    An array (that which you build with []) is something you can use to do linear algebra. One number in each element.

    A = [1 2 3;4 5 6;7 8 9];
    [3 5 7]*A*[2 3 5]'
    ans =
       915
    

    A cell array is a general container, that will hold any object, any matlab variable entirely in each cell. Thus we can create a cell array composed of elements of any shape and size.

    C = {'The' 'quick' 'brown' 'fox' 'jumps' 'over' 'the' 'lazy' 'dog'};
    

    C is a cell array with 9 elements in it. We can put any class of variable in there.

    C = {'asfghhrstyjtysj', 1:5, magic(4), sqrt(-1)}
    C = 
        'asfghhrstyjtysj'    [1x5 double]    [4x4 double]    [0 +          1i]
    

    We could even create a cell array where each cell contains only a single scalar number. But there would be no real point in doing so, as we cannot do arithmetic operations using cell arrays.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-03 04:15

    Elements of different data types which get inside {} become cells or elements of data type cell. Elements inside [] retain their data type and make an array of that data type. Few examples below:

    p = ['my', 'string'];
    q = [int8(1), int8(2), int8(3)];
    r = [0.11, 0.22, 0.33];
    s = {'my', 'string'};
    t = {1,2,3};
    u = {0.11, 0.22, 0.33};
    v = {int8(1), int8(2), int8(3)};
    
    >> whos
      Name      Size            Bytes  Class     Attributes
    
      p         1x8                16  char                
      q         1x3                 3  int8                
      r         1x3                24  double              
      s         1x2               240  cell                
      t         1x3               360  cell                
      u         1x3               360  cell                
      v         1x3               339  cell 
    
    0 讨论(0)
提交回复
热议问题