Matlab学习笔记[1](Creating Arrays)

1 CREATING A ONE-DIMENSIONAL ARRAY (VECTOR)

1.1 Creating a vector from a known list of numbers

例子

yr=[1984 1986 1988 1990 1992 1994 1996]

pop=[127; 130; 136; 145; 158; 178; 211]

Row vector

中间可以用space 或者用square brackets(,)号来隔开

Column vector

每一个Column要用semicolon或者用Enter换行隔开

 

1.2 Creating a vector with constant spacing

variable_name = [m:q:n]  Or variable_name = m:q:n

first term is m,last term is n,and the spacing is q

如果加q之后不能达到n,选择离n最近的

If only the first and the last terms,then the default for the spacing is 1

 

1.3 Creating a vector with linear

variable_name = linspace(xi,xf,n)

xi is First element

xf is Last element

n is Number of elements

When the number of elements is omitted, the default is 100

表示从xi到xf的vector,一共n个数(包括xi,xf)

 

1.4 CREATING A TWO-DIMENSIONAL ARRAY

 

variable_name=[1st row elements; 2nd row elements; 3rd row elements; ... ; last row elements]

行之间的用space 或者用square brackets(,)号来隔开

列之间的用semicolon来隔开

 

 

create a matrix with m rows and n columns which all elements are the numbers 0

zeros(m,n)

 

create a matrix with m rows and n columns which all elements are the numbers 1

ones(m,n)

 

 

creates a square matrix with n rows and n columns in which the diagonal elements are equal to 1 and the rest of the elements are 0 

eyes(n)

 

 

1.5 NOTES ABOUT VARIABLES IN MATLAB

在matlab中所有的变量都是数组,分别是scalar,vector and matrix

当变量被分配的时候,不需要分配其大小(before the elements are assigned)

scalar,vector and matrix这三种是可以随便再赋值的

 

1.6 The transpose operator

方法是X=Y‘

 

1.7 ARRAY ADDRESSING

1.7.1 Vector

VCT=[35 46 78 23 5 14 81 3 55];

 

VCT(4);

 

还可以这样给赋值va(m:n)

u=VCT(3:7)

 

还可以指定要那些,如

u=v([3, 5, 7:10])

 

1.7.2 Matrix

MAT=[3 11 6 5; 4 7 10 2; 13 9 0 8];

 

MAT(3,1)

 

还可以这样给赋值A(:,n),A(n,:),A(:,m:n),A(m:n,:),A(m:n,p:q)

 

A=[1 3 5 7 9 11;

      2 4 6 8 10 12;

      3 6 9 12 15 18;

      4 8 12 16 20 24;

      5 10 15 20 25 30]

B=A(:,3)

 

C=A(2,:)

E=A(2:4,:)

F=A(1:3,2:4)

 

还可以指定要那些,如

B = A([1,3],[1,3,5:7]) 1行,3行,1列,3列,5到7列

 

1.8 ADDING ELEMENTS TO EXISTING VARIABLES

vector

DF=1:4

DF(5:10)=10:5:35

 

AD=[5 7 2]

AD(8)=4 (其它地方补零)

 

AR(5)=24 (直接赋值的,其它地方直接补零)

 

 

matrix

E=[1 2 3 4; 5 6 7 8]

加一行

E(3,:)=[10:4:22]

 

AW=[3 6 9; 8 5 11]

AW(4,5)=17  (其它地方补零)

 

BG(3,4)=15 (直接赋值的,其它地方直接补零)

 

1.9 DELETING ELEMENTS

kt=[2 8 40 65 3 55 23 15 75 80]

kt(6)=[]

kt(3:6)=[]

 

mtr=[5 78 4 24 9; 4 0 36 60 12; 56 13 5 89 3]

mtr(:,2:4)=[]

 

1.10 BUILT-IN FUNCTIONS FOR HANDLING ARRAYS

length(A)

size(A)

等等查书和帮助文档

你可能感兴趣的:(matlab)