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

160 ビュー (過去 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 Center および File ExchangeCell Arrays についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by