Matlab Tutorial 2: Matrices in Matlab
Exercise 5: Combining matrices in Matlab
Earlier we have seen how we can select parts of a matrix like rows, columns or elements and how we canreshape a matrix to other matrices or vectors. Here we will try to do it the other way aroud. Lets assemble a matrix from vectors. Start by defining the following matrices/vectors:
>> A1=[ -2 3 4; 0 -2 5; 4 3 1];
>> a1=[ 3 4 5]
>> a2=[ 5 6 7]
>> b1=[ 1; 3; 5]
>> b2=[ 8; 5; 3]
Now let us build some new matrices from the matrix and vectors above.
>> a=[ a1 a2] % two row vectors put together. a= 3 4 5 5 6 7
>> b=[ b1 b2] % two column vectors put together. b= 8 5 3
Can we put together a matrix with vector, yes definitely. It works if they have the same number of rows or columns.
>> A1b1=[ A1 b1] % A1b1= -2 3 4 1 0 -2 5 3 4 3 1 5
Notice that [ A1 a1] would not have worked, but [ A1; a1] would have. The key is of course the inner product must be correct. Number of columns of A = number of rows of a1 Feel free to try other combinations of vectors and matrices.
Exercise 6: Matrix operations
Suppose we have two vectors x and y.
x = y= 1 2 0 5 3 8
How can we build the vectors z1 and z2 from x and y?
z1= z2=[ 1 0 3 2 5 8 ] 1 0 3 2 5 8
and
w1= 1 2 w2= 1 0 3 0 5 2 5 8 3 8
Related posts:
