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

157 次查看(过去 30 天)
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 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by