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

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

提交回复
热议问题