How to get average for each row in a cell array containing multiple matrices?
8 views (last 30 days)
Show older comments
Hi,
I have a cell array 19x21 called Hurst (see attachment) where each row contains differently sized cells. These cells contain matrices with a single row. I am trying to get means for each row in the cell array by averaging the matrices in each cell in each row.
I have tried using the cellfun(mean()) function but it throws the error:
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
The output I am trying to achieve should be a single column matrix.
Could someone please advise?
2 Comments
Arif Hoq
on 28 Jan 2022
Edited: Arif Hoq
on 28 Jan 2022
you can try with this:
clear;
clc;
A=load ('Hurst.mat');
A1=A.Hurst{1, 1};
A2=A.Hurst{1, 2};
A3=A.Hurst{2, 1};
A4=A.Hurst{2, 2};
% Replace the NaN value of Last cell with 0
A1(1,end)=NaN;
A1(isnan(A1))=0;
% find the average
A1_average=mean(A1);
A2(1,end)=NaN;
A2(isnan(A2))=0;
A2_average=mean(A2);
A3(1,end)=NaN;
A3(isnan(A3))=0;
A3_average=mean(A3);
A4(1,end)=NaN;
A4(isnan(A4))=0;
A4_average=mean(A4);
Accepted Answer
Voss
on 28 Jan 2022
load('Hurst.mat');
To average the matrices in each cell in Hurst:
cellfun(@mean,Hurst)
To average the matrices in each cell in Hurst, disregarding NaNs:
cellfun(@(x)mean(x,'omitnan'),Hurst)
To average those averages by row:
mean(cellfun(@(x)mean(x,'omitnan'),Hurst),2)
To concatenate all matrices in each given row, and average those:
arrayfun(@(ii)mean([Hurst{ii,:}],'omitnan'),(1:size(Hurst,1)).')
Note that the last result is equal to the second-to-last result because the matrices in each given row are all the same length. If they were to differ in length, the last two operations would potentially give different results (so you have to pick the one you want to do, which wasn't clear to me from the question):
x1 = 1:10;
x2 = 11:20;
m1 = mean(x1)
m2 = mean(x2)
mean([x1 x2]) % averaging the combined vector
mean([m1 m2]) % averaging the separate averages
x1 = [1:10 5.5 5.5 5.5 5.5]; % now longer but with the same mean
m1 = mean(x1)
m2 = mean(x2)
mean([x1 x2]) % averaging the combined vector
mean([m1 m2]) % averaging the separate averages
5 Comments
Voss
on 29 Jan 2022
load('Hurst.mat');
% first, keep track of some stuff for demonstration purposes:
% the number of elements of the matrices in each cell:
num_elements = cellfun(@numel,H_low);
disp(num_elements);
% the number of elements in each matrix that have a non-zero imaginary part:
num_complex = cellfun(@(x)nnz(imag(x)),H_low);
disp(num_complex);
% now perform the task of removing the complex numbers by keeping elements
% of the matrices in the cells of H_low that have imaginary part == 0:
H_low = cellfun(@(x)x(imag(x) == 0),H_low,'UniformOutput',false);
% verify that it worked by again counting the number of elements and the
% number of complex elements:
num_elements_new = cellfun(@numel,H_low);
num_complex_new = cellfun(@(x)nnz(imag(x)),H_low);
disp(num_elements_new);
disp(num_complex_new);
% make sure we got them all:
disp(~any(num_complex_new(:)));
% make sure we didn't remove anything we shouldn't have:
disp(isequal(num_elements_new,num_elements-num_complex));
More Answers (0)
See Also
Categories
Find more on Resizing and Reshaping Matrices 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!