How to get average for each row in a cell array containing multiple matrices?

8 views (last 30 days)
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
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);
lil brain
lil brain on 28 Jan 2022
Interesting @Mohammad Ariful Hoq.. thanks for your input! Unfortunately the cell array Hurst is a lot smaller than the actual array that I am working with. Hence the approach with defining A1 - A4 is probably not that practical. Maybe one needs to loop over the cells instead?

Sign in to comment.

Accepted Answer

Voss
Voss on 28 Jan 2022
load('Hurst.mat');
To average the matrices in each cell in Hurst:
cellfun(@mean,Hurst)
ans = 2×2
NaN NaN NaN NaN
To average the matrices in each cell in Hurst, disregarding NaNs:
cellfun(@(x)mean(x,'omitnan'),Hurst)
ans = 2×2
1.8226 1.7842 1.8135 1.6825
To average those averages by row:
mean(cellfun(@(x)mean(x,'omitnan'),Hurst),2)
ans = 2×1
1.8034 1.7480
To concatenate all matrices in each given row, and average those:
arrayfun(@(ii)mean([Hurst{ii,:}],'omitnan'),(1:size(Hurst,1)).')
ans = 2×1
1.8034 1.7480
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)
m1 = 5.5000
m2 = mean(x2)
m2 = 15.5000
mean([x1 x2]) % averaging the combined vector
ans = 10.5000
mean([m1 m2]) % averaging the separate averages
ans = 10.5000
x1 = [1:10 5.5 5.5 5.5 5.5]; % now longer but with the same mean
m1 = mean(x1)
m1 = 5.5000
m2 = mean(x2)
m2 = 15.5000
mean([x1 x2]) % averaging the combined vector
ans = 9.6667
mean([m1 m2]) % averaging the separate averages
ans = 10.5000
  5 Comments
Voss
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);
71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 150 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68
% 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);
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 2 0 3 2 3 3 2 4 0 4 5 4 5 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
% 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);
71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 72 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 76 149 150 149 150 150 150 150 150 150 150 150 150 150 150 149 149 150 150 150 150 149 73 73 73 73 73 73 73 71 73 70 71 70 70 71 69 73 69 68 69 68 70 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 85 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 69 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 77 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 56 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 49 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 39 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68
disp(num_complex_new);
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
% make sure we got them all:
disp(~any(num_complex_new(:)));
1
% make sure we didn't remove anything we shouldn't have:
disp(isequal(num_elements_new,num_elements-num_complex));
1
lil brain
lil brain on 29 Jan 2022
Superb! This works very well. Thanks for the explaining steps as well. I learned a lot!

Sign in to comment.

More Answers (0)

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!