Mean of values in acolumn depending on VARYING values of another column
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Freya
el 4 de Oct. de 2023
Comentada: Freya
el 5 de Oct. de 2023
Hi, I would like to calculate the mean of values in a coulmn, based on the values in another column. However, different from other questions asked previously on this forum, the values in the second column I need to use are not specific, but change. Please see attached file and hopefully it will be easier to get what I mean. Maybe I need to use a for loop running through all the values of the second column? Thank you!
0 comentarios
Respuesta aceptada
Dyuman Joshi
el 4 de Oct. de 2023
in =readmatrix('Book2.xlsx');
disp(in)
out = accumarray(in(:,2),in(:,1),[],@mean)
14 comentarios
Dyuman Joshi
el 5 de Oct. de 2023
Use the first output of unique()
format longg
in = readmatrix('Book3.xlsx');
[out1,~,idx] = unique(in(:,2))
out2 = accumarray(idx,in(:,1),[],@mean)
Concatenate them horizontally to get them in the same array
out = [out1 out2];
Más respuestas (1)
Ken Garrard
el 4 de Oct. de 2023
You do not need a for loop.
Assumming that your data is in an Nx2 variable named, mydata.
% Select rows matching criteria in column 2
% For example, values in column 2 greater than one and less than five
hits = mydata(:,2) > 1 & mydata(:,2) < 5;
% Mean of values from column 1
mean(mydata(hits,1))
ans =
12.7273
0 comentarios
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!