Question: Lab Assignment 2: Matrix and Vectors You should all have heard of or used Matrix and Vector before in your algebra classes. Matrix and Vectors

Lab Assignment 2: Matrix and Vectors

You should all have heard of or used Matrix and Vector before in your algebra classes. Matrix and Vectors are very useful tools in algebra and geometry alike. In this lab, you will implement some basic matrix and vector arithmetics. This lab is aimed to help you get familiar with the indexing system of arrays, and get comfortable with loops, and basic arithmetics.

Representation

As you all know, a vector of length n can be represented as an array of n numbers. Naturally, you can use an array to represent the vector.

A matrix has two dimensions, a row and a column. Normally, you might use a 2D array to represent a matrix. However, in this work, I want you to use a one dimension array to represent matrix. For example, a matrix of 3 by 4 (3 rows and 4 columns) has 12 elements. You will use an array of length 12 to represent this matrix. My suggestions is for you to use the first four numbers in the array to represent the first row of the matrix, then the second row, then the third row, etc. As each row has the same length, you can easily compute where the ith row starts (the indices). So, given any m by n matrix, you will use an array of size m*n to represent the matrix.

Addition and subtraction

The addition and subtraction of the vectors and matrix are simple: add or subtract the element at the corresponding locations.

Multiplication

The matrix multiplication is slightly more complicated than the addition. If you are not familiar with the rules of matrix multiplication, you should read here.

You should define a function that computes the matrix multiplication of the input matrices. You might want pass in the dimensions of the matrices to the function as well. One additional note: you matrix can have dimension of size 1, i.e., a vector. So, be sure to implement your functions as general as possible.

Matrix transpose

Matrix transpose is also a common operation in linear algebra. If you are not familiar with it, please read this.

Please implement a function that can transpose a matrix of any given size.

Row and column swap

Another common operation in the linear algebra is to swap different rows or columns of a matrix. For example, if given a matrix

Another common operation in the linear algebra is to swap different rows or columns of a matrix. For example, if given a matrix

1 2 3 4
5 6 7 8
9 10 11 12

one want to swap the first row and the third row, the matrix should become:

9 10 11 12
5 6 7 8
1 2 3 4

Column swap is similar. If on the original matrix, you want to swap column 2 and 4, the resulting matrix should be

1 4 3 2
5 8 7 6
9 12 11 10

So, you should implement a row_swap and column_swap function.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!