필터 지우기
필터 지우기

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

조회 수: 33 (최근 30일)
qqffssxx
qqffssxx 2023년 2월 24일
편집: Stephen23 2023년 2월 24일
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?

채택된 답변

VBBV
VBBV 2023년 2월 24일
[5 +6]
Is not equivalent to
5+6
When you use concatenate operator [ ]
  댓글 수: 2
VBBV
VBBV 2023년 2월 24일
편집: VBBV 2023년 2월 24일
But it's same when you use ( )
(5 +6)
Is equivalent to
5+6

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

qqffssxx
qqffssxx 2023년 2월 24일
I got it!
[1 +3 +5 ] == [1 3 5]

Askic V
Askic V 2023년 2월 24일
편집: Askic V 2023년 2월 24일
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 2023년 2월 24일
See this documentation page for more information.
  댓글 수: 1
Stephen23
Stephen23 2023년 2월 24일
편집: Stephen23 2023년 2월 24일
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.:

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by