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

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

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);
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

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

Dear Benjamin, thanks I appreciate the detailed feedback. One more thing, when I run
arrayfun(@(ii)mean([Hurst{ii,:}],'omitnan'),(1:size(Hurst,1)).')
on the my new Hurst.mat I receive a matrix (complex double) with complex or imaginary numbers (see attachment). Any idea why that is the case?
Thanks!
That is because there are complex values in some of the matrices in the original cell array. Specifically in the 4th and 5th rows:
load('Hurst.mat');
% count the number of elements in each matrix that have a non-zero imaginary part:
disp(cellfun(@(x)nnz(imag(x)),H_low))
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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
Inspecting one such matrix:
disp(H_low{4,1})
Columns 1 through 11 1.7728 + 0.0000i 1.8169 + 0.0000i 1.6746 + 0.0000i 1.7955 + 0.0000i 1.7773 + 0.0000i 1.8049 + 0.0000i 1.8622 + 0.0000i 1.7177 + 0.0000i 1.8929 + 0.0000i 1.7149 + 0.0000i 1.8762 + 0.0000i Columns 12 through 22 1.8714 + 0.0000i 1.6788 + 0.0000i 1.9039 + 0.0000i 1.8974 + 0.0000i 1.8643 + 0.0000i 1.8728 + 0.0000i 1.7661 + 0.0000i 1.8656 + 0.0000i 1.8325 + 0.0000i 1.8139 + 0.0000i 1.8909 + 0.0000i Columns 23 through 33 1.8361 + 0.0000i 1.8780 + 0.0000i 1.8918 + 0.0000i 1.7205 + 0.0000i 1.8084 + 0.0000i 1.8402 + 0.0000i 1.6673 + 0.0000i 1.9001 + 0.0000i 1.7203 + 0.0000i 1.8228 + 0.0000i 1.8675 + 0.0000i Columns 34 through 44 1.8764 + 0.0000i 1.8549 + 0.0000i 1.8756 + 0.0000i 1.8076 + 0.0000i 1.9086 + 0.0000i 1.8406 + 0.0000i 1.7816 + 0.0000i 1.7542 + 0.0000i 1.7892 + 0.0000i 1.8665 + 0.0000i 1.8004 + 0.0000i Columns 45 through 55 1.8738 + 0.0000i 1.8820 + 0.0000i 1.8992 + 0.0000i 1.9100 + 0.0000i 1.8727 + 0.0000i 1.8972 + 0.0000i 1.8289 + 0.0000i 1.6278 + 0.0000i 1.7641 + 0.0000i 1.7934 + 0.0000i 1.7760 + 0.0000i Columns 56 through 66 1.7843 + 0.0000i 1.8139 + 0.0000i 1.8322 + 0.0000i 1.9143 + 0.0000i 1.8978 + 0.0000i 1.6275 + 0.0000i 1.8666 + 0.0000i 1.8419 + 0.0000i 1.8506 + 0.0000i 1.6601 + 0.0000i 1.8372 + 0.0000i Columns 67 through 77 1.8651 + 0.0000i 1.8787 + 0.0000i 1.6059 + 0.0000i 1.7585 + 0.0000i 1.7489 + 0.0000i 1.7904 + 0.0000i 1.8057 + 0.0000i 1.9055 + 0.0000i 1.7611 + 0.0000i 1.8457 + 0.0000i 1.7845 + 0.0000i Columns 78 through 88 1.9045 + 0.0000i 1.7072 + 0.0000i 1.7410 + 0.0000i 1.8440 + 0.0000i 1.7276 + 0.0000i 1.8228 + 0.0000i 1.8917 + 0.0000i 1.8078 + 0.0000i 1.7844 + 0.0000i 1.7527 + 0.0000i 1.8539 + 0.0000i Columns 89 through 99 1.5047 + 0.0000i 1.9121 + 0.0000i 1.7875 + 0.0000i 1.7885 + 0.0000i 1.7521 + 0.0000i 1.7231 + 0.0000i 1.7950 + 0.0000i 1.6811 + 0.0000i 1.7002 + 0.0000i 1.7809 + 0.0000i 1.8108 + 0.0000i Columns 100 through 110 1.8781 + 0.0000i 1.8925 + 0.0000i 1.9163 + 0.0000i 1.8951 + 0.0000i 1.7381 + 0.0000i 1.8415 + 0.0000i 1.7796 + 0.0000i 1.8515 + 0.0000i 1.8083 + 0.0000i 1.6438 + 0.0000i 1.7061 + 0.0000i Columns 111 through 121 1.8851 + 0.0000i 1.4520 + 0.0000i 1.3928 + 0.0000i 1.3587 + 0.0000i 1.7461 + 0.0000i 1.8840 + 0.0000i 1.8311 + 0.0000i 1.8667 + 0.0000i 1.8899 + 0.0000i 1.8797 + 0.0000i 1.8792 + 0.0000i Columns 122 through 132 1.8017 + 0.0000i 1.8719 + 0.0000i 1.9028 + 0.0000i 1.8823 + 0.0000i 1.9102 + 0.0000i 1.3499 - 0.1203i 1.3564 + 0.0000i 1.8681 + 0.0000i 1.7611 + 0.0000i 1.7686 + 0.0000i 1.8885 + 0.0000i Columns 133 through 143 1.8520 + 0.0000i 1.8218 + 0.0000i 1.8631 + 0.0000i 1.7594 + 0.0000i 1.7937 + 0.0000i 1.8697 + 0.0000i 1.7784 + 0.0000i 1.8419 + 0.0000i 1.8399 + 0.0000i 1.9163 + 0.0000i 1.8390 + 0.0000i Columns 144 through 150 1.6389 + 0.0000i 1.6890 + 0.0000i 1.8402 + 0.0000i 1.8253 + 0.0000i 1.8843 + 0.0000i 1.7979 + 0.0000i NaN + 0.0000i
idx = find(imag(H_low{4,1}))
idx = 127
disp(H_low{4,1}(idx));
1.3499 - 0.1203i
Thank you! I wouldnt have noticed that. How would I go about removing the complex numbers from all of the cells but leaving the rest of the normal values?
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
Superb! This works very well. Thanks for the explaining steps as well. I learned a lot!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 28 Jan 2022

Commented:

on 29 Jan 2022

Community Treasure Hunt

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

Start Hunting!