find the max value of a mtrix in matlab

To find the maximum value of a matrix in MATLAB, you can use the max function. The max function returns the maximum value along a specified direction or in the entire matrix.

Here is an example:

main.m
% Create a 3x3 matrix
matrix = [1, 2, 3; 4, 5, 6; 7, 8, 9];

% Find the maximum value in the entire matrix
max_value = max(matrix(:));

disp(max_value);
153 chars
8 lines

Output:

main.m
9
2 chars
2 lines

In the above example, matrix(:) converts the matrix into a column vector, and max function is used to find the maximum value in the column vector.

You can also find the maximum value along a specific dimension of a matrix using the max function. For example, to find the maximum value of each column, you can specify the second dimension as the direction:

main.m
% Find the maximum value of each column
max_values = max(matrix, [], 2);

disp(max_values);
92 chars
5 lines

Output:

main.m
3
6
9
6 chars
4 lines

In the above example, max(matrix, [], 2) returns the maximum value of each column, and disp function is used to display the results.

related categories

gistlibby LogSnag