write a function that does the following calculation on an input: if the input is a non-square matrix, output it's dimensions in matlab

To write a function in MATLAB that performs the above calculation, you can do the following:

main.m
function output = matrix_dimensions(input_matrix)
% This function takes a matrix as input, and if it is non-square, outputs its dimensions
[m, n] = size(input_matrix);
if m~=n
    output = [m,n];
else 
    error('Input matrix is already square!')
end
251 chars
9 lines

Explanation:

  1. We define a function called matrix_dimensions that takes an input matrix as an argument
  2. We use the size function to get the dimensions of the input matrix
  3. We check if the dimensions m and n are equal, which means the matrix is square. In that case, we throw an error.
  4. If the matrix is non-square, we create a vector output with the dimensions m and n.

You can call this function with any non-square matrix to get its dimensions.

related categories

gistlibby LogSnag