user198729
user198729

Reputation: 63676

Is there any difference between [1 2] and [1 ,2] in MATLAB?

>> [1 2]

ans =

     1     2

>> [1 ,2]

ans =

     1     2

>> 

It looks the same,is that true?

Upvotes: 2

Views: 237

Answers (3)

yuk
yuk

Reputation: 19880

If you have doubt in the future test it by ISEQUAL function:

>> a=[1 2];
>> b=[1,2];
>> isequal(a,b)
ans =
     1

Upvotes: 0

user85109
user85109

Reputation:

Both produce a row vector when applied to scalar elements, i.e., horizontal concatenation. A space is equivalent to a comma inside square brackets to construct an array or vector. In fact, you can use spaces and commas at will within such an expression, although this may be best not done as it will be confusing to read. For example, this is difficult for me to read:

A = [1 2,3, 4 , 5 6 7, 8]

Far easier to read is either one of these alternatives:

A = [1 2 3 4 5 6 7 8]
A = [1,2,3,4,5,6,7,8]

Had you separated the elements with ; instead, this would produce vertical concatenation, which is a different animal. You can also build up arrays using these separators. So to create a 2x3 array,

A = [1 2 3;4 5 6]
A =
     1     2     3
     4     5     6

Upvotes: 4

Will Vousden
Will Vousden

Reputation: 33378

Nope; there's no difference. See here for more info:

The simplest way to create a matrix in MATLAB is to use the matrix constructor operator, []. Create a row in the matrix by entering elements (shown as E below) within the brackets. Separate each element with a comma or space:

row = [E1, E2, ..., Em]          row = [E1 E2 ... Em]

Upvotes: 8

Related Questions