MATLAB Answers

Creating an array from 1 to n

3.146 views (last 30 days)
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!

  1 Comment

Guillaume
Guillaume on 20 Dec 2019
This is such an elementary concept in matlab that I would recommend that you go through the free Matlab Onramp and the getting started tutorial in the doc to learn the basics of matlab.

Sign in to comment.

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 20 Dec 2019
n = 10 %
1:n
ans =
1 2 3 4 5 6 7 8 9 10

  0 Comments

Sign in to comment.

More Answers (1)

Suraj Sudheer Menon
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)=[]

  1 Comment

madhan ravi
madhan ravi on 26 Jun 2020
How’s this related to this thread?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by