Thread: mean, max, min in a 2d matrxi

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    43

    mean, max, min in a 2d matrxi

    i need helping finding the mean maximum, and minimum values for a two d matrix, yes i know this may be simple problem but i am not the best coder in the world i am stuck, so what i have so far is i am reading in an 2d matrix, which works perfectly and i need to find the maximum mean and minimum value i have an understanding on how to do this on a one d matrix but no a 2d matrix

    here is what i have so far,i read in the two d matrix and use two for loops to go through the rows and colloms


    [CODEor(r=0; r < no_of_rows; r++) {
    for(c=0; c < no_of_cols; c++) {

    }
    }][/CODE]

    what two do next i am lost, yes i do know what a mean value is, and i know that i need to read in every value from the no_of_row and no_of_cols and divide by the total number in each row and collum i also know i should probably have a variable set to zero and have it check each value in row the and collum keeping the bigger value but how to do that in software i am lost

    thanks for the help



    }
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Fix your code tags there.

    Since you know how to do this on a one-d array, you should know everything you need to know. The only difference is the extra dimension you need to access when comparing values.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    so here is my code so far, i am having trouble compling it i dont know why, but should this work this is just the function to find the min value i know that the max value would be the same

    Code:
    byte min_value;
    byte max_value;
    	
    min_value=image[r][c];
    
    for(r=0; r < no_of_rows; r++) {
         	    for(c=0; c < no_of_cols; c++) {
    				
    				if(min_value > image[r][c])
    				{
    					min_value=image[r][c];
    				}
    				
    
    				
                }
         	}
    
    return min_value;

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One problem, and a question:

    1) You are using r and c above the loop. Their value has not been assigned yet, so it's garbage. First assign r = c = 0; and then use them for min = array[r][c].

    Have you defined what "byte" is for the program with a typedef, already?
    Last edited by Adak; 03-26-2011 at 10:34 AM.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    here is my code all the way through, it complies fine but when i try and run the it it says the variable image is being used with out being assigned or blows up


    {
    byte **image; /* 2-d matrix data pointer */
    unsigned int r, /* row index */
    c, /* column index */
    bands; /* band index */

    unsigned int no_of_rows, /* number of rows in image */
    no_of_cols, /* number of columns in image */
    no_of_bands; /* number of image bands */

    Image* byteImage; /* Use for remapping, if needed */

    byte min_value;
    byte max_value;








    no_of_bands = getNoOfBands_Image(inputImage);

    /*
    ** Gets the number of rows in the input image
    */
    no_of_rows = getNoOfRows_Image(inputImage);

    /*

    no_of_cols = getNoOfCols_Image(inputImage);

    min_value=image[0][0];

    for(bands=0; bands < no_of_bands; bands++) {
    image = getData_Image(inputImage, bands);
    for(r=0; r < no_of_rows; r++) {
    for(c=0; c < no_of_cols; c++) {

    if(min_value > image[r][c])
    {
    min_value=image[r][c];
    }



    }
    }

    }

    return min_value;
    }

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You didn't bother to change the > sign (or use code tags). <===Never mind! < facepalm >

    Not really impressive, I'm afraid. 33 posts so far, you should know to use code tags.
    Last edited by Adak; 03-26-2011 at 10:33 AM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    He shouldn't change the sign, as that part is correct: if the current min_value is greater than some other matrix element, then it is clear we haven't found the minimum.

    it complies fine but when i try and run the it it says the variable image is being used with out being assigned or blows up
    I want to see your compiler's output. If it says image is being used without being assigned, then it does not "compile fine". It would be helpful to know what line that warning occurs on. Also, you're really not posting complete code here, so if this is all that you're compiling, it shouldn't link. You need a main() function for that to work.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Oh good grief! Thanks, Whiteflags!

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    43
    i do know i need a main, the main is just a giant case statement that class this function

    Run-Time Check Failure #3 - The variable 'image' is being used without being initialized.

    it gives me that error with the yellow arrow at the line min_value=image[0][0];

    i think i might have an issue when i call it in main i don't know if am passing in the right parameters to main

    the main function is as follows:
    void main(void)
    .......
    case14:
    meadium_filter_lab(inputImage)
    it then has some print functions that print the mean value
    i may have not a good understanding of passing in paramters, cause for the min value function in need the input image but then i am returning the mean value, so when i call it in main i know i need that value but what do i do about the min value i found, shouldn't i pass that into main as well

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Nope!

    You'll pass in the ADDRESS of min, and then, when you change it, inside your called function, it will change the value of min, even when the function has finished.

    declaring an array with zero size, is a warning - nothing will be done by the compiler. If all you want is a 2D pointer, then use int ** parray;
    Trying to use an array of no size, is an error, of course.

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Run-Time Check Failure #3 - The variable 'image' is being used without being initialized.

    it gives me that error with the yellow arrow at the line min_value=image[0][0];
    OK, so what loads the data into image? There is a serious problem there because it's not doing its job.

    i may have not a good understanding of passing in paramters, cause for the min value function in need the input image but then i am returning the mean value, so when i call it in main i know i need that value but what do i do about the min value i found, shouldn't i pass that into main as well
    First, I doubt its a parameter type issue that's causing your problems: if the argument doesn't match the parameter type, then you would be getting a compiler error, not a run time error. There is a problem with the way you load the image, so you need to debug that. For some reason, image[0][0] is garbage, and it's a fair bet that other parts of the matrix are also garbage. Find out why and fix this problem.

    Second, I think that min, max, and mean should all be their own function. Normally you would return the min, the max, and the mean as a return value, but since you have only one return value in C or C++, you have limited choices. Either
    a) make these three things separate functions
    b) pass in pointer variables to min, max, and mean storage
    c) return a structure variable containing the min, max and mean

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  3. Passing a 2d array by Reference
    By loko in forum C Programming
    Replies: 8
    Last Post: 07-23-2005, 06:19 AM
  4. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM
  5. 2D array sorting from min. to max.
    By khaled_helmy in forum C Programming
    Replies: 1
    Last Post: 10-14-2004, 02:17 PM