Matrices and Vectors

时间:2022-10-02 08:33:34

Matrices and Vectors

Matrices are 2-dimensional arrays:

Matrices and Vectors

A vector is a matrix with one column and many rows:The above matrix has four rows and three columns, so it is a 4 x 3 matrix.

Matrices and Vectors

Notation and terms:So vectors are a subset of matrices. The above vector is a 4 x 1 matrix.

  • Aij refers to the element in the ith row and jth column of matrix A.
  • A vector with 'n' rows is referred to as an 'n'-dimensional vector.
  • vi refers to the element in the ith row of the vector.
  • In general, all our vectors and matrices will be 1-indexed. Note that for some programming languages, the arrays are 0-indexed.
  • Matrices are usually denoted by uppercase names while vectors are lowercase.
  • "Scalar" means that an object is a single value, not a vector or matrix.
  • R refers to the set of scalar real numbers.
  • Rn refers to the set of n-dimensional vectors of real numbers.

Run the cell below to get familiar with the commands in Octave/Matlab. Feel free to create matrices and vectors and try out different things.

% The ; denotes we are going back to a new row.
A = [1, 2, 3; 4, 5, 6; 7, 8, 9; 10, 11, 12] % Initialize a vector
v = [1;2;3] % Get the dimension of the matrix A where m = rows and n = columns
[m,n] = size(A) % You could also store it this way
dim_A = size(A) % Get the dimension of the vector v
dim_v = size(v) % Now let's index into the 2nd row 3rd column of matrix A
A_23 = A(2,3)

Addition and Scalar Multiplication

Addition and subtraction are element-wise, so you simply add or subtract each corresponding element:

Matrices and Vectors

Subtracting Matrices:

Matrices and Vectors

In scalar multiplication, we simply multiply every element by the scalar value:To add or subtract two matrices, their dimensions must be the same.

Matrices and Vectors

Experiment below with the Octave/Matlab commands for matrix addition and scalar multiplication. Feel free to try out different commands. Try to write out your answers for each command before running the cell below.In scalar division, we simply divide every element by the scalar value:

Matrices and Vectors

Experiment below with the Octave/Matlab commands for matrix addition and scalar multiplication. Feel free to try out different commands. Try to write out your answers for each command before running the cell below.

% Initialize matrix A and B
A = [1, 2, 4; 5, 3, 2]
B = [1, 3, 4; 1, 1, 1] % Initialize constant s
s = 2 % See how element-wise addition works
add_AB = A + B % See how element-wise subtraction works
sub_AB = A - B % See how scalar multiplication works
mult_As = A * s % Divide A by s
div_As = A / s % What happens if we have a Matrix + scalar?
add_As = A + s

Matrix-Vector Multiplication

We map the column of the vector onto each row of the matrix, multiplying each element and summing the result.

Matrices and Vectors

An m x n matrix multiplied by an n x 1 vector results in an m x 1 vector.The result is a vector. The number of columns of the matrix must equal the number of rows of the vector.

Below is an example of a matrix-vector multiplication. Make sure you understand how the multiplication works. Feel free to try different matrix-vector multiplications.

% Initialize matrix A
A = [1, 2, 3; 4, 5, 6;7, 8, 9] % Initialize vector v
v = [1; 1; 1] % Multiply A * v
Av = A * v

Matrix-Matrix Multiplication

We multiply two matrices by breaking it into several vector multiplications and concatenating the result.

Matrices and Vectors

To multiply two matrices, the number of columns of the first matrix must equal the number of rows of the second matrix.An m x n matrix multiplied by an n x o matrix results in an m x o matrix. In the above example, a 3 x 2 matrix times a 2 x 2 matrix resulted in a 3 x 2 matrix.

For example:

% Initialize a 3 by 2 matrix
A = [1, 2; 3, 4;5, 6] % Initialize a 2 by 1 matrix
B = [1; 2] % We expect a resulting matrix of (3 by 2)*(2 by 1) = (3 by 1)
mult_AB = A*B % Make sure you understand why we got that result

Matrix Multiplication Properties

  • Matrices are not commutative: A∗B≠B∗A
  • Matrices are associative: (A∗B)∗C=A∗(B∗C)

The identity matrix, when multiplied by any matrix of the same dimensions, results in the original matrix. It's just like multiplying numbers by 1. The identity matrix simply has 1's on the diagonal (upper left to lower right diagonal) and 0's elsewhere.

Matrices and Vectors

When multiplying the identity matrix after some matrix (A∗I), the square identity matrix's dimension should match the other matrix's columns. When multiplying the identity matrix before some other matrix (I∗A), the square identity matrix's dimension should match the other matrix's rows
.

% Initialize random matrices A and B
A = [1,2;4,5]
B = [1,1;0,2] % Initialize a 2 by 2 identity matrix
I = eye(2) % The above notation is the same as I = [1,0;0,1] % What happens when we multiply I*A ?
IA = I*A % How about A*I ?
AI = A*I % Compute A*B
AB = A*B % Is it equal to B*A?
BA = B*A % Note that IA = AI but AB != BA

Inverse and Transpose

The inverse of a matrix A is denoted A−1. Multiplying by the inverse results in the identity matrix.

A non square matrix does not have an inverse matrix. We can compute inverses of matrices in octave with the pinv(A) function and in Matlab with the inv(A) function. Matrices that don't have an inverse are singular or degenerate.

The transposition of a matrix is like rotating the matrix 90° in clockwise direction and then reversing it. We can compute transposition of matrices in matlab with the transpose(A) function or A':

Matrices and Vectors

In other words:

Matrices and Vectors

% Initialize matrix A
A = [1,2,0;0,5,6;7,0,9] % Transpose A
A_trans = A' % Take the inverse of A
A_inv = inv(A) % What is A^(-1)*A?
A_invA = inv(A)*A

Matrices and Vectors的更多相关文章

  1. RNN 入门教程 Part 2 – 使用 numpy 和 theano 分别实现RNN模型

    转载 - Recurrent Neural Networks Tutorial, Part 2 – Implementing a RNN with Python, Numpy and Theano 本 ...

  2. [zt]矩阵求导公式

    今天推导公式,发现居然有对矩阵的求导,狂汗--完全不会.不过还好网上有人总结了.吼吼,赶紧搬过来收藏备份. 基本公式:Y = A * X --> DY/DX = A'Y = X * A --&g ...

  3. Applying Eigenvalues to the Fibonacci Problem

    http://scottsievert.github.io/blog/2015/01/31/the-mysterious-eigenvalue/ The Fibonacci problem is a ...

  4. 图像处理之image stitching

    背景介绍 图像拼接是一项应用广泛的图像处理技术.根据特征点的相互匹配,可以将多张小视角的图像拼接成为一张大视角的图像,在广角照片合成.卫星照片处理.医学图像处理等领域都有应用.早期的图像拼接主要是运用 ...

  5. 对于fmri的设计矩阵构造的一个很直观的解释-by 西南大学xulei教授

    本程序意在解释这样几个问题:完整版代码在本文的最后. 1.实验的设计如何转换成设计矩阵? 2.设计矩阵的每列表示一个刺激条件,如何确定它们? 3.如何根据设计矩阵和每个体素的信号求得该体素对刺激的敏感 ...

  6. Introduction to Gaussian Processes

    Introduction to Gaussian Processes Gaussian processes (GP) are a cornerstone of modern machine learn ...

  7. The Model Complexity Myth

    The Model Complexity Myth (or, Yes You Can Fit Models With More Parameters Than Data Points) An oft- ...

  8. SparkMLlib-----GMM算法

    Gaussian Mixture Model(GMM)是一个很流行的聚类算法.它与K-Means的很像,但是K-Means的计算结果是算出每个数据点所属的簇,而GMM是计算出这些数据点分配到各个类别的 ...

  9. Machine-learning of Andrew Ng(Stanford University)

    1.基础概念 机器学习是一门研究在非特定编程条件下让计算机采取行动的学科.最近二十年,机器学习为我们带来了自动驾驶汽车.实用的语音识别.高效的网络搜索,让我们对人类基因的解读能力大大提高.当今机器学习 ...

随机推荐

  1. Yii2-多表关联的用法示例

    本篇博客是基于<活动记录(Active Record)>中对于AR表关联用法的介绍. 我会构造一个业务场景,主要是测试我比较存疑的各种表关联写法,而非再次介绍基础用法. 构造场景 订单ar ...

  2. 【Java EE 学习 22 上】【文件上传】【目录打散】【文件重命名】

    1.文件上传概述 (1)使用<input type="file">的方式来声明一个文件域. (2)表单提交方式一定要是post方式才行 (3)表单属性enctype 默 ...

  3. Sharepoint client model 中出现Cannot invoke HTTP DAV request&period; There is a pending query 的解决办法

    由于近期在某项目中使用sharepoint client 对象模型做项目 在sharepoint 2010环境下正常,但迁移到sharepoint 2013后报错,提示如下 Cannot invoke ...

  4. 合唱队形2(洛谷U5874)

    题目背景 上次老师挑出来的(N-K)位同学很不高兴,于是他们准备自己组建合唱队形.他们请了kkk来帮忙. 题目描述 他们安排了一个动作--手拉着手唱一首歌(就是他们围成一个圈).如果有两个相邻的同学的 ...

  5. IOS中 如何去除Tabview里面cell之间的下划线

    可以利用Tabview的separatorStyle属性来设置,选择其中的UITableViewCellSeparatorStyleNone 即可去除cell之间的下划线 self.tableView ...

  6. Flex编程注意之直接获取某个组件的对象(this&lbrack;&rsqb;用法)通过id获取控件

    有这样一个需求:假如你new了一百次Button,同时这些button的id分别赋值如btn1.id = "button1"; btn2.id = "button2&qu ...

  7. 书法字帖 PDF转化为可打印PDF

    书法类的PDF,因为底色是黑色的,打印起来特别费墨,所以需要转化成白底黑字的文件, 才好打印. 1)用 pdfbox 的 ExtractImages 命令,抽出所有的图片 https://pdfbox ...

  8. &lbrack;Android 开发教程(1)&rsqb;-- Saving Data in SQL Databases

    Saving data to a database is ideal for repeating or structured data, such as contact information. Th ...

  9. 认识 Thymeleaf

  10. TabControl中显示和隐藏TabPage页

    在使用TabControl控件时,希望隐藏其中某个选项卡(即TabPage).TabPage类明明提供了一个Hide方法,用在代码中却没有任何效果,甚是奇怪.无奈之余,只好考虑另辟途径 方法一: 设置 ...