In: Mechanical Engineering
For the following, explain what each of them will do in MatLab by saying what the command does and what the result is. For example, if the command is >>x(2:end, 1:3) then your answer to this problem should be: This command retrieves the subset of x defined by rows 2 and 3 and columns 1-3. The result is a 2x3 matrix. Before you begin, run the command: x = [1 3 4 5 7; 5 6 10 9 11; 10 23 23 1 3];
a) x(:,5)
b) x(1,:)
c) x(2,3:4)
d) x(1:2,2:5)
e) length(x(1,:))
f) length(x(:,1))
g) sum(x(2,:))
h) mean(x(:,end))
i) max(x)
j) max(max(x))
k) round(3.75)
l) floor(3.75)
m) rem(150,100)
n) log(25)
a):
x(:,5): This command retrieves all the elements in 5th column of the x. The result is a 3x1 matrix.
The output in command window is:
7
11
3
b):
x(1,:): This command retrieves all the elements in 1st row of the x. The result is a 1x5 matrix.
The output in command window is:
1 3 4 5 7
c):
x(2,3:4): This command retrieves all the elements in 2nd row and 3rd and 4th columns of the x. The result is a 1x2 matrix.
The output in command window is:
10 9
d):
x(1:2,2:5): This command retrieves all the elements from 1st to 2nd rows and from 2nd to 5th columns of the x. The result is a 2x4 matrix.
The output in command window is:
3 4 5 7
6 10 9 11
e):
length(x(1,:)): This command gives the length of first row of x. In other words, it gives the number of columns of x.
The output in command window is:
5
f):
length(x(:,1)): This command gives the length of first column of x. In other words, it gives the number of rows of x.
The output in command window is:
3
g):
sum(x(2,:)): This command gives the sum of all elements in second row of x.
The output in command window is:
41
h):
mean(x(:,end)): this gives the mean value of all the elements of the last column of x. It is the sum of all elements in the last row divided by number of elements in last row, i.e., (7+11+3)/3 = 7
The output in the command window is:
7.
i):
max(x): This command gives a row vector with the maximum elements in each of the columns of x. Since x has 5 columns, the size of the output vector is 1x5
The output in the command window is:
10 23 23 9 11
j):
max(max(x)): This command gives the maximum value among the maximum elements in each of the columns of x. In other words, it gives the value of the maximum element of x.
The output in the command window is:
23
k):
round(3.75): This command gives the closest integer value to the given value. In this case the closest integer is 4. If the fraction part is 0.5, for the positive number it gives the next highest integer and in case of a negative number, it gives next lowest integer.
The output in the command window is:
4
l):
floor(3.75): This command gives the closest integer value tha is less than the given value. In this case the closest integer is 3. In other words, it rounds off the given number towards negative infinity.
The output in the command window is:
3
m):
rem(150,100): This command gives the remainder obtained when the first argument is divided by the second argument. In this case, it is 50.
The output in the command window is:
50
n):
log(25): This command gives the natural logarithamic value of the input argument, i.e.,
In this case it is
The output in the command window is:
3.2189