Jump to content
  • Advertisement
Sign in to follow this  
serious_learner07

Mean of the columns in a matrix

This topic is 3594 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I need to take mean of each column of a matrix(MxN) I have the code given below. But I am getting some junk values as mean. Whats wrong with my logic.

#include <stdio.h>

#define N 3
#define M 2

void Compute_Mean(float* observations, float* mean)
{
	float sum;

	for(int i=0;i<M;i++)
	{
		sum = 0.0f;
		for(int j=0;j<N;j++)
		{
                        //accessing the column.
			printf("%f\n",observations[j*N+i]);
			sum += observations[j*M+i];
		}
		mean = sum/M;	
	}
}

int  main()
{
        /* inputting   
	float *array = new float[N*M];
        /* stores the mean of the columns */ 
	float *mean = new float[M];

        //Intializing the array. 
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<M;j++)
		{
			array[i*N+j] = (float)(i+j);
		}
	}
	
	Compute_Mean(array,mean);

}

Share this post


Link to post
Share on other sites
Advertisement
You are confusing M and N; why not call them NUMBER_OF_ROWS and NUMBER_OF_COLUMNS, and declare them as const int instead of using dangerous macros?

mean sould be sum/N (divide by the length of the inner loop), and the index in printf is wrong as well.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!