# Creating an array from 1 to n

2,469 views (last 30 days)
Agnieszka Polak on 20 Dec 2019
Commented: Hans Henning Erle on 17 Nov 2021
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!
Stephen on 27 Jul 2021
@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 on 20 Dec 2019
n = 10 %
1:n
ans =
1 2 3 4 5 6 7 8 9 10
##### 2 CommentsShowHide 1 older comment
Hans Henning Erle on 17 Nov 2021
10:-1:0

Suraj Sudheer Menon on 26 Jun 2020
Edited: madhan ravi on 26 Jun 2020
B=sort(A);
for i=2:numel(B)
if(B(i)==B(i-1)
B(i)=false;
end
end
B(B==false)=[];
A(A==B)=[]
madhan ravi on 26 Jun 2020
How’s this related to this thread?