AVERAGE OF EACH COLUMNS IN CELL

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'?

Answers (2)

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 Comments

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 on 28 Apr 2019
Edited: Image Analyst on 28 Apr 2019
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.

Sign in to comment.

I need the average of each column for PCA.

1 Comment

To get the average of each column, use mean() on your matrix M:
averageOfColumns = mean(M, 1);

Sign in to comment.

Categories

Asked:

on 28 Apr 2019

Commented:

on 28 Apr 2019

Community Treasure Hunt

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

Start Hunting!