How to get average for each row in a cell array containing multiple matrices?
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
lil brain
el 28 de En. de 2022
Comentada: lil brain
el 29 de En. de 2022
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 comentarios
Arif Hoq
el 28 de En. de 2022
Editada: Arif Hoq
el 28 de En. de 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);
Respuesta aceptada
Voss
el 28 de En. de 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 comentarios
Voss
el 29 de En. de 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));
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!