Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between square brackets and curly brackets in Matlab?

Tags:

matlab

A bit of a newbie question: whats the difference between square brackets [] and curly brackets {} in Matlab? When is it appropriate to use either?

Update: its actually in the Matlab docs under "Special Characters".

like image 861
Contango Avatar asked May 11 '11 15:05

Contango


People also ask

What do curly brackets mean in MATLAB?

Cell Array Syntax Cell arrays in Matlab use the curly bracket {} notation instead of the normal parentheses (). While you may think that using () works, it in fact returns the "cell" of the array, not the "value" of the cell, which 99% of the time is not what you are looking for.

What does [] mean in MATLAB?

Description: Square brackets enable array construction and concatenation, creation of empty matrices, deletion of array elements, and capturing values returned by a function.

What are {} used for in MATLAB?

Special Characters [ ] ( ) {} = ' . ... , ; % ! Brackets are used to form vectors and matrices. [6.9 9.64 sqrt(-1)] is a vector with three elements separated by blanks.

What's the difference between square brackets and round brackets?

Usually we use square brackets - [ ] - for special purposes such as in technical manuals. Round brackets - ( ) - are used in a similar way to commas when we want to add further explanation, an afterthought, or comment that is to do with our main line of thought but distinct from it.


2 Answers

A square bracket creates a vector or matrix, whereas curly brackets creates a cell array.

When working with numbers, I'd say that 99% of the time, you will use square brackets. Cell arrays allow you to store different types of data at each location, e.g. a 10x5 matrix at (1,1), a string array at (1,2), ...

x = [1 2 3]; #% matrix with values 1, 2, 3 y = {1, 'a', x}; #% cell array storing a number, a character, and 1x3 matrix 

Here is the MATLAB documentation on cell arrays: http://www.mathworks.com/help/matlab/cell-arrays.html

like image 142
AVH Avatar answered Sep 23 '22 19:09

AVH


This paper answered my question above in a very elegant manner. The paper explains Matlab arrays to someone who is more familiar with non-array based languages such as C++, C#, Java, and Python:

MATLAB array manipulation tips and tricks - Peter J. Acklam

like image 40
Contango Avatar answered Sep 21 '22 19:09

Contango