AVERAGE OF EACH COLUMNS IN CELL
Show older comments
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)
Image Analyst
on 28 Apr 2019
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
Aybüke Ceren Duran
on 28 Apr 2019
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.
Aybüke Ceren Duran
on 28 Apr 2019
0 votes
1 Comment
Image Analyst
on 28 Apr 2019
To get the average of each column, use mean() on your matrix M:
averageOfColumns = mean(M, 1);
Categories
Find more on Data Types in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!