>> 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 the same thing?
>> 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 the same thing?
{}
's are for cells. []
's are for arrays/matrices.
[]
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.
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.
If you relate it to object oriented programming, cells {} are like objects and [] is for arrays
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