Finding the greatest number in each row a matrix

  • Thread starter Thread starter physicophile
  • Start date Start date
  • Tags Tags
    Matrix Row
AI Thread Summary
The discussion centers on finding the maximum number in each row of a matrix using C code. The original code shared by the user has several issues, particularly with the initialization of variables and the logic used to iterate through the matrix. Participants suggest improvements, such as using the first element of each row as the initial maximum value to reduce the number of comparisons needed. The use of DBL_MIN for initialization is questioned, as it may not be necessary. Additionally, there are recommendations to include checks for special cases like NaN (Not a Number) and INF (infinity) to make the code more robust. Overall, the conversation emphasizes optimizing the approach to finding row maxima while ensuring the code handles edge cases effectively.
physicophile
Messages
19
Reaction score
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
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:
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?
 
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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top