Imagine,
N=input("Enter the number of storeys: ")
m1=input("what is you m1 value? ")
As you see in the first command i ask a user to input the number of storeys. Now, my question is that i want the m1 input command to pop up N times which was entered in the first command. How can I possibly do this?
Can anyone help me on this basic issue? Thank you!

답변 (1개)

Deepak Gupta
Deepak Gupta 2020년 5월 26일

0 개 추천

If you want to enter the values one by one, then you can use a loop:
N=input("Enter the number of stories: ")
for index = 1:N
m1(index)=input("What is your m1 value? ")
end

댓글 수: 5

Murad Nuri
Murad Nuri 2020년 5월 26일
Thank you for your response! Yes it is kind of what you are saying.
But to be exact, is it possible to have
m1=input("What is your m1 value? ")
m2=input("What is your m1 value? ")
...
mN=input("What is your m1 value? ")
N times that i inputted in the first command?
Deepak Gupta
Deepak Gupta 2020년 5월 26일
No, it's not possible as Value of N will be entered later by the user so program doesn't know in advance the value of N. Only possible way is that you use if else loops or switch case but that will be highly redundant and can cover only a finite values.
Steven Lord
Steven Lord 2020년 5월 26일
If you're asking if you can create N individual variables, one to store each individual m1 value, you can do this but you should not. Prefer storing them as elements in one larger variable.
Murad Nuri
Murad Nuri 2020년 5월 26일
Thank you both for your help.
What is the reason I should not do that. Furthermore, am I able to choose from the general variable m vector assigned? I mean I tried the code preferred by Mr. Gupta, and the assigned m vector has appeared to be a vector with N number of values.
Steven Lord
Steven Lord 2020년 5월 26일
See this answer for some of the many reasons to avoid creating many individual scalar variables with numbered names.

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2020년 5월 26일

댓글:

2020년 5월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by