Matlab Basics: Matrix Creation, Operations and Indexing

Matrix Creation

Directly declare a matrix using semicolons to separate different rows.

>> a = [1 2 3; 4 5 6; 7 8 9]

a =

     1     2     3
     4     5     6
     7     8     9

You can also quickly create a matrix with functions such as ones, zeros and rand.
For instance, ones(3,4) is to create a 3-by-1 column matrix of ones.

>> b = ones(3,4)

b =

     1     1     1     1
     1     1     1     1
     1     1     1     1

Matrix Operations

MATLAB allows you to process all of the values in a matrix using a single arithmetic operator or function.

>> a+10

ans =

    11    12    13
    14    15    16
    17    18    19

>> sin(a)

ans =

    0.8415    0.9093    0.1411
   -0.7568   -0.9589   -0.2794
    0.6570    0.9894    0.4121

Operators:

A = [1 2 3 4; 5 6 7 8]
B = [1 1 2 2; 2 2 1 1]
C = A + B
D = A - B
E = A * B'
F = A .* B
G = A / B     % B * G = A
H = A ./ B

Clearly * is a matrix operation, and .* is element-wise.

Concatenation:

>> A = [a,a]

A =

     1     2     3     1     2     3
     4     5     6     4     5     6
     7     8     9     7     8     9

Indexing

Indexing is to get you the selected element.

>> a(2,3)

ans =

     6

>> a(2,:)

ans =

     4     5     6

>> a(2,1:2:3)

ans =

     4     6

MATLAB ® Primer © COPYRIGHT 1984–2017 by The MathWorks, Inc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值