Plus as concatenate for numeric vectors?

조회 수: 32 (최근 30일)
Sebastian
Sebastian 2025년 11월 17일 15:36
편집: dpb 2025년 11월 17일 21:24
I've been using Matlab for years and I've never seen this before, is it new? It was hard to debug this since it's pretty subtle
size([ones(1,10) +ones(1,10)])
ans = 1×2
1 20
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Apparently if you don't put a space after the plus sign -- and there is a space before it -- then + concatenates instead of adding. There's nothing in the documentation for + about concatenating numeric arrays. I don't really have a question, this just seems really confusing as a feature.

채택된 답변

dpb
dpb 2025년 11월 17일 15:51
편집: dpb 2025년 11월 17일 21:24
No it's not new. In that expression the plus sign is interpreted as the sign of the value, not as an arithmetic operator.
It's no different than
[1 +1]
ans = 1×2
1 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
or
[1 -1]
ans = 1×2
1 -1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
the latter of which wouldn't surprise you -- in fact, you'd be very disconcerted if it produced the same as
[1-1]
ans = 0
or
[1 - 1]
ans = 0

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by