1284 0

[Weka及其他] MATLAB课程:代码示例之Working with Arrays and Matrices(一) [推广有奖]

企业贵宾

已卖:160份资源

巨擘

0%

还不是VIP/贵宾

-

威望
4
论坛币
624047 个
通用积分
180.4857
学术水平
918 点
热心指数
987 点
信用等级
841 点
经验
399143 点
帖子
9786
精华
48
在线时间
17322 小时
注册时间
2014-8-19
最后登录
2022-11-2

楼主
widen我的世界 学生认证  发表于 2016-3-4 14:59:53 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

MATLAB课程:代码示例之Working with Arrays and Matrices(一)


Basic Matrix Operations


This example shows basic techniques and functions for working with matrices in the MATLAB® language.


First, let's create a simple vector with 9 elements called a.

a = [1 2 3 4 6 4 3 4 5]


a =     1     2     3     4     6     4     3     4     5


Now let's add 2 to each element of our vector, a, and store the result in a new vector.

Notice how MATLAB requires no special handling of vector or matrix math.

b = a + 2


b =     3     4     5     6     8     6     5     6     7


Creating graphs in MATLAB is as easy as one command. Let's plot the result of our vector addition with grid lines.

plot(b)grid on


MATLAB can make other graph types as well, with axis labels.

bar(b)xlabel('Sample #')ylabel('Pounds')


MATLAB can use symbols in plots as well. Here is an example using stars to mark the points. MATLAB offers a variety of other symbols and line types.

plot(b,'*')axis([0 10 0 10])


One area in which MATLAB excels is matrix computation.

Creating a matrix is as easy as making a vector, using semicolons (;) to separate the rows of a matrix.

A = [1 2 0; 2 5 -1; 4 10 -1]


A =     1     2     0     2     5    -1     4    10    -1


We can easily find the transpose of the matrix A.

B = A'


B =     1     2     4     2     5    10     0    -1    -1


Now let's multiply these two matrices together.

Note again that MATLAB doesn't require you to deal with matrices as a collection of numbers. MATLAB knows when you are dealing with matrices and adjusts your calculations accordingly.

C = A * B


C =     5    12    24    12    30    59    24    59   117


Instead of doing a matrix multiply, we can multiply the corresponding elements of two matrices or vectors using the .* operator.

C = A .* B


C =     1     4     0     4    25   -10     0   -10     1


Let's use the matrix A to solve the equation, A*x = b. We do this by using the \ (backslash) operator.

b = [1;3;5]x = A\b


b =     1     3     5x =     1     0    -1


Now we can show that A*x is equal to b.

r = A*x - b


r =     0     0     0


MATLAB has functions for nearly every type of common matrix calculation.

There are functions to obtain eigenvalues ...

eig(A)


ans =    3.7321    0.2679    1.0000


... as well as the singular values.

svd(A)


ans =   12.3171    0.5149    0.1577


The "poly" function generates a vector containing the coefficients of the characteristic polynomial.

The characteristic polynomial of a matrix A is


p = round(poly(A))


p =     1    -5     5    -1


We can easily find the roots of a polynomial using the roots function.

These are actually the eigenvalues of the original matrix.

roots(p)


ans =    3.7321    1.0000    0.2679


MATLAB has many applications beyond just matrix computation.

To convolve two vectors ...

q = conv(p,p)


q =     1   -10    35   -52    35   -10     1


... or convolve again and plot the result.

r = conv(p,q)plot(r);


r =     1   -15    90  -278   480  -480   278   -90    15    -1


At any time, we can get a listing of the variables we have stored in memory using the who or whos command.

whos


  Name      Size            Bytes  Class     Attributes  A         3x3                72  double                B         3x3                72  double                C         3x3                72  double                a         1x9                72  double                ans       3x1                24  double                b         3x1                24  double                p         1x4                32  double                q         1x7                56  double                r         1x10               80  double                x         3x1                24  double              


You can get the value of a particular variable by typing its name.

A


A =     1     2     0     2     5    -1     4    10    -1


You can have more than one statement on a single line by separating each statement with commas or semicolons.

If you don't assign a variable to store the result of an operation, the result is stored in a temporary variable called ans.

sqrt(-1)


ans =   0.0000 + 1.0000i


As you can see, MATLAB easily deals with complex numbers in its calculations.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Matrices MATLAB课程 matrice working matric MATLAB课程 代码示例 WorkingwithArraysandMatrices Basic Matrix


https://www.cda.cn/?seo-luntan
高薪就业·数据科学人才·16年教育品牌

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-22 15:24