Where is documentation for colon range of the form 1:vector?

2 ビュー (過去 30 日間)
Paul Hoffrichter
Paul Hoffrichter 2023 年 6 月 13 日
コメント済み: Stephen23 2023 年 6 月 13 日
I think the following code examples should result in a Matlab error and not be allowed to run. I think the code syntax should be disallowed by Matlab.
Where is the documentation that describes the use of the colon operator having the following form? All the examples in https://www.mathworks.com/help/matlab/ref/colon.html appear to use only scalar integers.
I was reviewing code that had code analogous to:
a = [ 3 9 44];
b = 1:a
b = 1×3
1 2 3
I didn't recognize this form. The results appear to show that only the first element of a is used. Where is the documentation that allows this?
A quick experiment shows that the first element sof both vectors are used when creating a range:
a = [2 12 11];
b = [ 13 19 144];
c = a:b
c = 1×12
2 3 4 5 6 7 8 9 10 11 12 13

採用された回答

Steven Lord
Steven Lord 2023 年 6 月 13 日
The third item in the Description section of the documentation page to which you link describes what the colon, : operator does when its inputs are non-scalar.
"x = j:i:k creates a regularly-spaced vector x using i as the increment between elements. The vector elements are roughly equal to [j,j+i,j+2*i,...,j+m*i] where m = fix((k-j)/i). However, if i is not an integer, then floating point arithmetic plays a role in determining whether colon includes the endpoint k in the vector, since k might not be exactly equal to j+m*i. If you specify nonscalar arrays, then MATLAB interprets j:i:k as j(1):i(1):k(1)." [Emphasis added.]
  4 件のコメント
Paul Hoffrichter
Paul Hoffrichter 2023 年 6 月 13 日
Stephen23
Stephen23 2023 年 6 月 13 日
This should definitely be deprecated. And also that counter-productive column-iteration behavior of FOR.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by