In: Computer Science
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started
%x is the input vector or matrix. You can find the
%size that is the number of rows and the number
%of columns of x by using the size function in MATLAB function transpose_ret = findTranspose(x)
. . .
%transpose_ret is the variable that will contain the transpose of the input x end
Matlab function can be defined with the help of number of rows and number of columns which is given as input to justify the size of the matrix and also input of the matrix x.This can be done by giving input of any matrix or any vector.
MATLAB code snippet
r = input('Enter the no. of rows');
c = input('Enter no. of columns');
function transpose_ret = findTranspose(x)
[r c] = size(x);
transpose_ret = zeros(c,r);
for i = 1:r
for j = 1: c
transpose_ret(j,i) = x(i,j)
end
end
end
x = [1 3;2 5]; %x is given as input 2x2 matrix
transpose_ret = findTranspose(x); % transpose the matrix x
transpose_ret
OUTPUT
Enter the no. of rows> 2 Enter no. of columns> 2 transpose_ret = 1 0 0 0 transpose_ret = 1 0 3 0 transpose_ret = 1 2 3 0 transpose_ret = 1 2 3 5 transpose_ret = 1 2 3 5
This can also be verified by using the transpose function
x = [1 3;2 5]; %x is given as input 2x2 matrix
transpose(x)
OUTPUT
ans = 1 2 3 5