How do I make empty lists in MATLAB Live Script and append to it?

조회 수: 271 (최근 30일)
NiNa
NiNa 2018년 7월 1일
답변: Madeline Gardner 2018년 7월 3일
I want to make 3 empty list, in which I can append values to as shown in the following (The code is written in Python):
na=[]
nb=[]
t=[]
na.append(float(raw_input("the initial number of nuclei A:")))
ta=float(raw_input("the constant time of nuclei A:"))
nb.append(float(raw_input("the initial number of nuclei B:")))
tb=float(raw_input("the constant time of nuclei B:"))
tt=float(raw_input("the total time:"))
dt=float(raw_input("the time step:"))
t.append(0)
I do not know how this is done in MATLAB. Please help.

답변 (1개)

Madeline Gardner
Madeline Gardner 2018년 7월 3일
Hello!
Luckily, Python and MATLAB have pretty similar syntax, so your code is actually going to look pretty similar! You'll be using the function input which, unlike Python, can actually return a float (or a 'double', which is one type of floating-point number in MATLAB) and not just a string. And adding to an array in MATLAB is also very easy, since MATLAB is designed around arrays, vectors, and matrices -- you can just put brackets around the original array and the new elements, like so [array, elem] to concatenate them.
So, your code will follow the general form:
na = []
x = input("the initial number of nuclei A:")
na = [na x] % No need to change the type of x
I hope that helps! Also, if you're new to MATLAB, there are some very helpful tutorials to get you started here: https://www.mathworks.com/help/matlab/getting-started-with-matlab.html

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by