Borrar filtros
Borrar filtros

What is the answer of [(4 -2 ) /2 , 5 +6 , 7 , 8 ] ? Why does MATLAB output [1 5 6 7 8]?

33 visualizaciones (últimos 30 días)
When the plus sign is preceded by a blank space:
[(4 -2 ) /2 , 5 +6 , 7 , 8 ]
ans = 1×5
1 5 6 7 8
Delete blank space:
[(4 -2 ) /2 , 5+6 , 7 , 8 ]
ans = 1×4
1 11 7 8
I don't understand why MATLAB will output different results.
Under normal circumstances, shouldn't spaces be ignored?

Respuesta aceptada

VBBV
VBBV el 24 de Feb. de 2023
[5 +6]
Is not equivalent to
5+6
When you use concatenate operator [ ]
  2 comentarios
VBBV
VBBV el 24 de Feb. de 2023
Editada: VBBV el 24 de Feb. de 2023
But it's same when you use ( )
(5 +6)
Is equivalent to
5+6

Iniciar sesión para comentar.

Más respuestas (3)

qqffssxx
qqffssxx el 24 de Feb. de 2023
I got it!
[1 +3 +5 ] == [1 3 5]

Askic V
Askic V el 24 de Feb. de 2023
Editada: Askic V el 24 de Feb. de 2023
In Matalb, when you define an array (or vector) you can use the following syntax:
v = [1 3 4 5 6]
v = 1×5
1 3 4 5 6
This is exactly the same as:
v = [1, 3, 4, 5, 6]

Steven Lord
Steven Lord el 24 de Feb. de 2023
See this documentation page for more information.
  1 comentario
Stephen23
Stephen23 el 24 de Feb. de 2023
Editada: Stephen23 el 24 de Feb. de 2023
There is nothing on that page that explicitly explains how space is interpreted around arithmetic operators when building arrays:
[1 - 2]
ans = -1
[1 -2]
ans = 1×2
1 -2
This topic comes up fairly regularly on Answers, indicative of confusion regarding this topic, and that page looks like a good place to document it. None of the other "obvious" pages I found in a quick search now covered this topic either, e.g.:

Iniciar sesión para comentar.

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by