Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between 5 and [5] in MATLAB?

Tags:

matlab

They seem to be exactly the same, like:

>> v1 = [5];
>> v2 = 5;
>> isequal(v1, v2)
ans = 
      1
>> [5] * [1,2,3]
ans = 
       5   10   15
>> v1(1)
ans = 
      5
>> v2(1)
ans = 
      5

Is there any differenceI should be aware of?

Thanks.

like image 859
totally Avatar asked Jan 13 '16 18:01

totally


2 Answers

Is there any difference I should be aware of?

No

like image 72
sco1 Avatar answered Nov 12 '22 03:11

sco1


Although there is no significant difference, there is a difference.

v1=5; is creating a variable called v1 that has a value of 5.

v1=[5]; is defining a matrix/scalar with the value of 5; then it is concatenating that matrix with nothing - concatenation is the operation performed by the square brackets, and is why you do need them to define [1,2,3] - and then the result is assigned to the variable v1. So using the square brackets performs an additional operation.

This is why if you write your code in the editor you'll receive an m-lint message saying

The use of brackets [] is unnecessary. Use parenthesis to group, if needed.

like image 29
Phil Goddard Avatar answered Nov 12 '22 05:11

Phil Goddard