In: Computer Science
Use Matlab to introduce arow vector,f,and define it as a 1 by 4 vector showing values of 1, 2, 3and 4. Now try to build matrix D,
using the following 2 commands
a.D = [f ; A]
b.D= [f , A]
c.Which one can be built,and which one cannot be built? Explain your answer.
Matrix A, which is a 4by 4 matrix, (The first, second, third and fourth column values are:2,4,6,8and 1,3,5,7 and 8,6,4,2 and 7,5,3,1 , respectively.
Answer
Here is your answer, Here we can create a 1x4 vector with values 1 2 3 4, So, here we need to create a matrix called D, with concatenate one vector and one matrix called f and A. Actually in this above we need to show that which command is valid or not. So here is your matlab code,
using D=[f;A];
f=[1 2 3 4]; %created a 1x4 vector f
A=[2,4,6,8;1,3,5,7 ;8,6,4,2 ;7,5,3,1];%actually this is row values
A=A';%find transpose for make it is column value
D=[f;A]; %valid concatenation
%concatenate the rows of the f with rows of A , dimension should be equal, columns number should be equal
ouput
using D=[f,A];
f=[1 2 3 4]; %created a 1x4 vector f
A=[2,4,6,8;1,3,5,7 ;8,6,4,2 ;7,5,3,1];%actually this is row values
A=A';%find transpose for make it is column value
D=[f;A]; %invalid concatenation in the case of f and A, dimension problem
%concatenate the rows of the f with rows of A , dimension should be equal, columns number should be equal
it will produce the following error
This happend beacuse horzcat(), which trying horizontally concatenates two incompatible input matrices.
any doubt please comment
Thanks in advance