Thread Closed

Finding the greatest number in each row a matrix

 
Share Thread
Apr30-08, 10:56 PM   #1
 

Finding the greatest number in each row a matrix


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);
}}
PhysOrg.com science news on PhysOrg.com

>> City-life changes blackbird personalities, study shows
>> Origins of 'The Hoff' crab revealed (w/ Video)
>> Older males make better fathers: Mature male beetles work harder, care less about female infidelity
May1-08, 10:15 AM   #2
 
Recognitions:
Gold Membership Gold Member
Science Advisor Science Advisor
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;
}
May1-08, 04:50 PM   #3
 
Admin
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
--
http://www.chembuddy.com
http://www.ph-meter.info
May2-08, 06:55 AM   #4
 
Recognitions:
Gold Membership Gold Member
Science Advisor Science Advisor

Finding the greatest number in each row a matrix


Quote by Borek View Post
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 in to the mix. This code is devoid of checks. INF and NAN values may cause problems
Thread Closed

Similar discussions for: Finding the greatest number in each row a matrix
Thread Forum Replies
Complex Number Covariance Matrix General Math 0
min number dependent columns in a matrix Linear & Abstract Algebra 8
Condition number (or estimate) of a very large matrix Linear & Abstract Algebra 0
Matrix, making R2 to R3, finding standard matrix A! where did i mess up? Calculus & Beyond Homework 1
finding the inverse and finding a matrix * A = 0 matrix Introductory Physics Homework 6