1. Consider the following 3 × 4 matrix and define it in MATLAB Using appropriate
ID: 671095 • Letter: 1
Question
1. Consider the following 3 × 4 matrix and define it in MATLAB
Using appropriate MATLAB functions do the following, each with a single MATLAB command:
a. Determine the minima of the columns and their locations. Then, determine the minima of the rows and their locations without transposing the matrix. Then, repeat for the maxima of the columns and rows.
b. Compute the column means. Compute the row means without transposing the matrix. Compute the column sums; then, the row sums. Compute the column products; then, the row products.
c. Apply the functions cumsum, cumprod, and diff on the matrix A column-wise, and then apply them on A, row-wise.
d. Sort A column-wise in decreasing order. Sort A row-wise in increasing order.
e. Replace by zeros the 2×2 submatrix defined at the intersection of the second and third columns and the second and third rows of A.
f. Flip the matrix A upside-down. Then, flip A left-right. Combine the previous two operations into one that flips A both upside-down and left-right.
g. Replace the main diagonal (i.e., the A11, A22, . . . , matrix elements) by zeros. Replace all elements of A by zeros except for its elements in the upper-left 2×2 submatrix.
h. Swap the second and fourth columns of A. Swap the first and third rows of A.
i. Rearrange A into a 2×6 matrix whose elements in each column are taken column-wise from those of A. Similarly, reshape A into a 4×3 matrix and compare it with the transpose of A. Then, reshape A into a 6×2 matrix. Finally, reshape A into a 2×2×3 three-dimensional array.
j. Using the function repmat, replicate A three times horizontally. Replicate A two times vertically. Replicate A three times horizontally and two times vertically. Re-do these replications without the use of repmat, just by appropriately concatenating A with itself.
k. What do the commands, A==5, and, q = find(A==5), do? With one command, replace all the entries of A that are equal to 5 by the value of 50. Then, with another single command restore their values back to 5.
l. Insert the row vector [10, 20, 30, 40] between rows 2 and 3 of the matrix A without re-typing the matrix elements of A. Insert the column [10; 20; 30] between columns 2 and 3 of the matrix A.
m. Delete simultaneously the third column of A and its second row (without re-typing any matrix elements).
Explanation / Answer
% index vectors for rows and columns p = 1:m; q = 1:n; % index matrices for rows and columns [P, Q] = ndgrid(p, q); % create a matrix with the shift values K = repmat(k(:), [1 n]); % update the matrix with the column indexes Q = 1 + mod(Q+K, n); % create matrix of linear indexes ind = sub2ind([m n], P, Q); % finally, create the output matrix B = A(ind);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.