AVERAGE OF EACH COLUMNS IN CELL

15 次查看(过去 30 天)
I have 'data' variable which is cell. I want to take the average of each column in 'data'. How can I take average of column in 'data' and subtract average from the 'data'?

回答(2 个)

Image Analyst
Image Analyst 2019-4-28
Try this (untested):
dataContents = data{1} % Extract array from a cell called data . "data" was NOT called a cell array so I assume it's only one cell.
% Compute averages of columns
columnAverages = mean(dataContents, 1); % Average going down rows within columns.
% Subtract column average from each columns
[rows, columns] = size(dataContents);
% Expland to matrix the same size as the dataContents matrix.
columnAverages = repmat(columnAverages, [rows, 1]); % Legacy way of doing it that should work with older versions.
output = dataContents - columnAverages; % Do the subtraction
  2 个评论
Aybüke Ceren Duran
In the line:
columnAverages = mean(dataContents, 1); % Average going down rows within columns.
It says:
Index in position 1 exceeds array bounds.
Image Analyst
Image Analyst 2019-4-28
编辑:Image Analyst 2019-4-28
Is it possible that you called your m-file "mean.m"? Or you have an array in your code called mean? I think that might cause the error. Otherwise, tell me what data is, or attach it in a mat file with the paper clip icon.

请先登录,再进行评论。


Aybüke Ceren Duran
I need the average of each column for PCA.
  1 个评论
Image Analyst
Image Analyst 2019-4-28
To get the average of each column, use mean() on your matrix M:
averageOfColumns = mean(M, 1);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by