How to find largest coefficient in matrix?

234 Views Asked by At

$$\begin{bmatrix} 1&2&6\\ 7&8&3\\ 0&4&7 \end{bmatrix} $$

I want know the algorithm to find largest value in matrix .

2

There are 2 best solutions below

4
On

Generally, to make the algorithm by yourself, you need to use two loops to check all the entities of the matrix for a comparative approach.

3
On

In C code:

int matrix[] = {1, 2, 6, 7, 8, 3, 0, 4, 7};
int min = matrix[0];
for(i = 1; i < sizeof(matrix); i = i+1) {
  if(matrix[i] < min)
    min = matrix[i];
}

If you would also like to get which element is the min:

int matrix[] = {1, 2, 6, 7, 8, 3, 0, 4, 7};
int min = matrix[0];
int min_row = 1;
int min column = 1;
int row_size = 3;
for(i = 1; i < sizeof(matrix); i = i+1) {
  if(matrix[i] < min) {
    min = matrix[i];
    min_row = (i/3) + 1;
    min_column = (i % 3) + 1; 
  }
}

Here, (min_row, min_column) is the element of the minimum value.