When to use {}, [], or () …

 

{}  Use to define AND index cell arrays.  These are use exclusively for cell arrays.  Recall cell arrays can store elements of different size and/or type.

 

 

Defining cell array:

x={ 'one ', 'two ', 'three ',4,5,6}

 

Indexing cell array:

x{1}

x{4}

________________________________________________________________________

 

[]  Use to define a standard array.  Used to create an array.  Often used to create an array from the output of a function.

 

y=[1:10]

 

y=['a', 'b', 'c']

 

y=[1 2 3

      4 5 6

     7 8 9]

 

[x,fs]=wavread('filename.wav');

 

__________________________________________________________

 

() Use to index a standard array and to provide inputs to functions

 

z=[5,7,9,2,4,5];

 

z(1)                           z( [1,3,4] )

z(3)

z(2:4)

z(:)

 

A=[1 2 3 4 5

      6 7 8 9 10

      1 2 3 4 5];

 

A(1,1)                                    A( [2,3], [1,2,4] )

A( 1:2,1:2)

A(:,1)

A(1,:)

 

[x,fs]=wavread('filename.wav');

sin(x)

cos(x)