Finding the greatest number in each row a matrix

  • Thread starter physicophile
  • Start date
  • Tags
    Matrix Row
In summary: To be closer to bulletproof: isnan() or something like it should be in there too. Which adds more overhead back into the mix. This code is devoid of checks. INF and NAN values may cause problems.
  • #1
physicophile
19
0
Hi, everybody. Could some one please help find the greatest number in each row of a matrix, this is what i have so far.. am i one the right track? (using C code)




Float big;
Big = -9999999.99999

for( i==0) {
for( j = 0; j < n; j++) {
if( *(mat + (j*0) + 0) > big) {
bigc1 = *(mat + 0*j + 0);
}}
for( i==1) {
for( j = 0; j < n; j++) {
if( *(mat + (j*1) + 1) > big) {
bigc2 = *(mat + 1*j + 1);
}}
for( i==2) {
for( j = 0; j < n; j++) {
if( *(mat + (j*2) + 2) > big) {
bigc3 = *(mat + 2*j + 2);
}}
 
Technology news on Phys.org
  • #2
There were enough issues to warrant a full example for a matrix [n] x [3]:
Code:
/* max per "row" */
#include <stdlib.h>
#include <limits.h>  /* we need DBL_MIN */

/* find max in each row of a matrix or table */
void max_per_row( double *result, 
                          double matrix[][3], 
                          const int xmax, 
                          const int ymax)
{
	int x=0, y=0;

	for(y=0; y < ymax; y++)
		result[y]=DBL_MIN;
	for(y=0; y < ymax; y++)
	{
		for(x=0; x < xmax; x++)
		{
			if ( matrix[y][x] > result[y] )
				result[y]=matrix[y][x];
		}
	}
}

int main(int argc, char **argv)
{
	double test1[3][3]={ {-10, -20, 0},
                         {-13, 0,  13},
                         {-99, 99.99, 100.01}};
    double result[10]={0};
    int y=0;
    max_per_row(result, test1, 3, 3 );
    for(y=0; y < 3 ; y++)
    	printf("max row %d = %f\n", y, result[y]);
    return 0;
}
 
Last edited:
  • #3
Why do you (both) use DBL_MIN? If you are looking for the greatest number you can use first number from the row as an initial value. That saves one comparison per each row. Or am I missing something?
 
  • #4
Borek said:
Why do you (both) use DBL_MIN? If you are looking for the greatest number you can use first number from the row as an initial value. That saves one comparison per each row. Or am I missing something?

I implemented the OP's program in the way he wrote it. Yes, using the first value does eliminate one comparison.

Code:
         for(x=1, result[y]=matrix[y][0]; x < xmax; x++)
		{
			if ( matrix[y][x] > result[y] )
				result[y]=matrix[y][x];
		}

To be closer to bulletproof: isnan() or something like it should be in there too. Which adds more overhead back into the mix. This code is devoid of checks. INF and NAN values may cause problems
 

1. What is a matrix?

A matrix is a rectangular array of numbers, symbols, or expressions arranged in rows and columns.

2. How do you find the greatest number in a matrix?

To find the greatest number in a matrix, you need to compare each number in the matrix to the others and determine which one is the largest. This can be done by either visually inspecting the matrix or using a computer program.

3. What is the significance of finding the greatest number in a matrix?

Finding the greatest number in a matrix can be useful in many applications, such as finding the maximum value in a dataset or identifying the largest element in a problem-solving scenario.

4. Is there a specific method for finding the greatest number in a matrix?

There are several methods for finding the greatest number in a matrix, including visually inspecting the matrix, using a computer program, or using mathematical algorithms such as the maximum or minimum functions.

5. Can the greatest number in a matrix be found in more than one row?

Yes, the greatest number in a matrix can be found in more than one row. In some cases, there may be multiple numbers in different rows that are tied for the greatest value.

Similar threads

  • Programming and Computer Science
Replies
1
Views
889
  • Programming and Computer Science
Replies
3
Views
1K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
Replies
9
Views
938
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
1K
  • Programming and Computer Science
Replies
2
Views
868
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top