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.
Is there any difference I should be aware of?
No
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With