Project

Coding Basics

1. A=[1,2,3,4,5]

This will generate a row vector. The output will be- [1 2 3 4 5]

2. B=[1;2;3;4;5]

This will generate a column vector, such that the entry after semi-colon will be entered in a new row. The output of this statement will be-

1

2

3

4

5

3. C = A*B'

This will multiply matrix A with the transpose of matrix B. Output is only possible in this case when the number of columns of A is equal to number of columns in B.

e.g. A is a m*n matrix and B is a k*n matrix. Transpose of B is B' which will be a n*k matrix. The resultant matrix C will be of dimension m*k.

4. C = B*A

This will multiply matrix B with matrix A. Output is only possible in this case when the number of columns of B is equal to number of rows in A.

e.g. If B is a m*n matrix and A is a n*x matrix. The resultant matrix C will be of dimension m*x.

5. A(1:end)

This is a case of Linear indexing. MATLAB uses column-wise ordering of data. This means when data is filled in a matrix, it will initially enter the 1st column and then the 2nd column and so on. And same rule applies for indexing also when we call out the elements from a matrix.

Suppose A is a 3*3 matrix as follows-

1 2 3

4 5 6

7 8 9

In MATLAB memory, the data is stored like- 1 is the 1st element, 4 is 2nd element, 7 is the 3rd, 2 is the 4th, and so on. This is column-wise ordering of data.

So, when we execute the given statement, i.e A(1:end), the output will be all the elements stored in the memory of A in 1st, 2nd...upto the last position.

Output is- 1 4 7 2 5 8 3 6 9

6. A(1:2)

Similar to the above logic, here we get the output as the elements which are stored in 1st and 2nd memory of A.

Output is- 1 4

7. B(1:2,3)

Suppose B is a 3*3 matrix as follows-

1 2 3

4 5 6

7 8 9

This is case of 2-dimensional indexing. The matrix recognizes the data stored in it by means of the number of rows and columns. Generally, while calling an element from matrix, the first number is the row and second one is indication of column. Here the output will return two elements- First will be the element present at position of 1st row and 3rd column and Second will be element present at position of 2nd row and 3rd column

Output is-

3

6

8. B(length(B)-2)

length(B) will return the number of columns present in the matrix. As per the above case, answer is 3. Then substracting 3-2=1, the statement then becomes B(1). This is linear indexing which means the element stored at 1st position in B.

Output is- 1

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.