Week two - code basics
1) A=[1,2,3,4,5]
Output:
A =
1 2 3 4 5
Here 'A' is a variable to store a particular value,
'=' is an operator used to assign value to a variable,
[] braces indicates values are in a matrix form,
',' indicates column division
1,2,3,4,5 are elements of matrix size 1X5
2) B=[1;2;3;4;5]
Output: B =
1
2
3
4
5
Here 'B' is a variable to store a particular value,
'=' is an operator used to assign value to a variable,
[] braces indicates values are in a matrix form,
';' indicates rows division
1,2,3,4,5 are elements of matrix size 5X1
3) C = A*B'
Output 1:
C =
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Here above command had tried to compute product of 2 matrices but as number of columns in the first matrix
did't match with the number of rows in the second matrix it showed error.
It is asking us to use '.*' for dot product.
Output 2:
Unrecognized function or variable 'A'.
For above output I have used 'clear all' so this demands me to assign values to variables A and B used.
4) 4. C = B*A
Output 1:
C =
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Here above statement has performed Matrix multiplication by taking already assigned value of A and B.
Output 2:
Unrecognized function or variable 'B'.
For above output I have used 'clear all' so this demands me assign values to variables A and B used.
5. A(1:end)
Output 1:
ans =
1 2 3 4 5
It has used inbuilt variable to store the result i.e, 'ans' as we are not mentioned.
Here above statement has printed elements of variable A from 1st element upto the last element.
Here 'end' is an operator which has got predefined meaning in matlab.
Output 2:
The end operator must be used within an array index expression.
For above output I have used 'clear all' so this demands me to assign values to variable A.
6) A(1:2)
ans =
1 2
It will print index of particular element in matrix A here it will print 1st row elements starting from first element to second element and no column elements as 'comma; not used.
Output 2:
Unrecognized function or variable 'A'.
For above output I have used 'clear all' so this demands me assign values to variable A.
7) B(1:2,3)
Output 1:
Index in position 2 exceeds array bounds (must not exceed 1).
As 'B' is a column matrix of size 5X1, here 1:2 indicates row no 1 and 2 and 3 indicates 3rd column. As it is a single column matrix there is no third column so error occured.
Output 2:
Unrecognized function or variable 'B'.
For above output I have used 'clear all' so this demands me assign values to variable B.
8) B(length(B)-2)
ans =
3
Here, in the above statement length command returns highest length of matrix 'B' for eg. if it is a 3X4 matrix it returns 'length' as 4 but in this case it is 5X1 matrix so it returns 5. Now it is B(5-2)=B(3) whis is 3rd row 1st element which is 3.
Output 2:
Unrecognized function or variable 'B'.
For above output I have used 'clear all' so this demands me assign values to variable B.