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

Réponses (2)

Image Analyst
Image Analyst le 28 Avr 2019

0 votes

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 commentaires

Aybüke Ceren Duran
Aybüke Ceren Duran le 28 Avr 2019
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 le 28 Avr 2019
Modifié(e) : Image Analyst le 28 Avr 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.

Connectez-vous pour commenter.

Aybüke Ceren Duran
Aybüke Ceren Duran le 28 Avr 2019

0 votes

I need the average of each column for PCA.

1 commentaire

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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Types dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by