How to find average of column of matrix?

3 visualizaciones (últimos 30 días)
Gargi
Gargi el 13 de Oct. de 2014
Editada: Gargi el 13 de Oct. de 2014
I have a matrix of 12 columns and 36 rows . I want to do the average of all the columns and get the results separately. I've attached the data file with this. Please, find the attachment . Also, 've to do with the help of loops, not direct command.
  2 comentarios
Stephen23
Stephen23 el 13 de Oct. de 2014
Loops? Why?
Image Analyst
Image Analyst el 13 de Oct. de 2014
For reasons I don't completely understand or agree with, often professors want students to not use built in functions. In fact we have an item on that : http://www.mathworks.com/matlabcentral/answers/38787-what-can-be-programmed-without-any-built-in-functions I guess the professors think it helps them learn it better but it seems dumb to me. Heck, why not just make them write it in Java or C or assembly then?

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 13 de Oct. de 2014
You didn't attach anything, so I'll just assume you can use csvread() or importdata() to get your data into an array m. Then to do the average with loops, not functions like mean() or sum():
Also, like Mike I don't know what you mean by "average of all the columns" - it's ambiguous. It could mean for each row, get the average over all the columns in that row. Or it could mean for each column, get the average of all the values (which span all the rows in the column). I'm going to assume you want one mean for each column and average over all the rows in each column.
[rows, columns] = size(m);
for col = 1 : columns
theSum = 0;
for row = 1 : rows
theSum = theSum + m(row, col);
end
% Now get the mean over all values in this column.
columnMeans(col) = theSum / rows;
end
  5 comentarios
Image Analyst
Image Analyst el 13 de Oct. de 2014
It does work. Just look:
% Get the full filename, with path prepended.
folder = 'D:\Temporary stuff';
baseFileName = 'data.xls';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
numbers = xlsread(fullFileName);
[rows, columns] = size(numbers);
for col = 1 : columns
theSum = 0;
for row = 1 : rows
theSum = theSum + numbers(row, col);
end
% Now get the mean over all values in this column.
columnMeans(col) = theSum / rows;
end
columnMeans
In the command window:
columnMeans =
Columns 1 through 7
-1.84602188888889 -3.69086624166667 -6.59312922222222 -13.0488363888889 -16.9640358333333 -24.6707722222222 -34.4893333333333
Columns 8 through 12
-45.6823138888889 -49.955 -57.6123472222222 -57.4078333333333 -62.2485138888889
It's identical to what I posted, I just changed the variable name from m to "numbers" and added a call to xlsread() to read in your data. What did you do differently? Somehow you broke the code I gave you but I don't know how because you forgot to post it. Post it if you want to learn why yours doesn't work. Otherwise please mark the answer as "Accepted."
Gargi
Gargi el 13 de Oct. de 2014
Editada: Gargi el 13 de Oct. de 2014
Thanku Sir. My mistake was in this line "[rows, columns] = size(m);", ve done a silly mistake in writing size(m), I just forgot what to write in size(m). But now 've understood it perfectly. Thanku Sir....!!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by