Creating an array from 1 to n

1,921 ビュー (過去 30 日間)
Agnieszka Polak
Agnieszka Polak 2019 年 12 月 20 日
コメント済み: Hans Henning Erle 2021 年 11 月 17 日
Hello!
I am trying to create an array of integers from 1 to n [1 2 3 ..... n], where n is a variable that can change.
How should I approach that?
Thank you!
  3 件のコメント
Luis Benitez Prieto
Luis Benitez Prieto 2021 年 7 月 27 日
It is elemantary but also not. If you look at the accepted answer below. The answer is creating a double array, the numbers inside are integer values, but not of interger format.
I actually was interested in this question because when I want to create an array of integers I do not want to create a double array in the process by doing something like:
uint32([1:10])
Instead, I would like to directly create an integer array. And thats something I have not easily found in the documentation and a legit question in my opinion. Perhaps thats what the poster is asking for.
Stephen23
Stephen23 2021 年 7 月 27 日
編集済み: Stephen23 2021 年 7 月 27 日
@Luis Benitez Prieto: like this:
V = uint32(1):uint32(10)
V = 1×10
1 2 3 4 5 6 7 8 9 10
class(V)
ans = 'uint32'
Note that just one uint32 input is sufficient to define the output as uint32:
class(uint32(1):10)
ans = 'uint32'
class(1:uint32(10))
ans = 'uint32'
Note that the square brackets in your example are superfluous and misleading:

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

採用された回答

Alex Mcaulley
Alex Mcaulley 2019 年 12 月 20 日
n = 10 %
1:n
ans =
1 2 3 4 5 6 7 8 9 10
  2 件のコメント
Rui
Rui 2021 年 10 月 30 日
編集済み: Rui 2021 年 10 月 30 日
how can i get it in reverse order also with a 0 in end?
like this
10 9 8 7 6 5 4 3 2 1 0
Hans Henning Erle
Hans Henning Erle 2021 年 11 月 17 日
10:-1:0

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by