So I am trying to make a program that a user can input a starting variable (n0), and how many numbers in want to view (n), and it will display that in this format.
n0=5, n=10
5,6,7,8,9,10,11,12,13,14
I REALLY want to use a for loop, but im open to suggestions. Thanks!
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is what i have so far, and its not even close :
%%User input
N0=input('What should n0 be? ');
N=input('What should n be? ');
a(1)=N0;
%%For loop
for ix=N0:1:N
x(is)=(ix)
end
Making it into a string is going to be the second hard part.

 채택된 답변

Stephen23
Stephen23 2020년 9월 2일

0 개 추천

>> n0 = 5;
>> n = 10;
>> c = sprintf(',%d',n0+(0:n-1));
>> c = c(2:end)
c = 5,6,7,8,9,10,11,12,13,14

댓글 수: 3

Lance Hartley
Lance Hartley 2020년 9월 2일
Is there a way to do it using a for loop?
Stephen23
Stephen23 2020년 9월 2일
"Is there a way to do it using a for loop?"
Of course: you could just concatenate the new data on each loop iteration. It would not be as efficient though.
Give it a try and show us what you come up with!
Lance Hartley
Lance Hartley 2020년 9월 2일
Everything works! Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 9월 2일

댓글:

2020년 9월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by