if matrix is longer than wide, rotate by 90 degrees in matlab

To rotate a matrix in MATLAB such that it is longer than wide, you can use the combination of transpose and flipud functions as shown in the code snippet below:

main.m
% sample matrix
matrix = [1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15];

% if matrix is longer than wide, rotate by 90 degrees
if size(matrix,1) > size(matrix,2)
    matrix = flipud(matrix');
end
192 chars
8 lines

In the code above, we check if the number of rows is greater than the number of columns. If this is true, we first transpose the matrix using the ' operator, and then reverse the order of rows using the flipud function. The result is a rotated matrix.

gistlibby LogSnag