mean calculation by comparison
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Thishan Dharshana Karandana Gamalathge
el 25 de Abr. de 2015
Comentada: Mohammad Abouali
el 25 de Abr. de 2015
Hi, In the following vectors, I need to get the "mean" of y values for each set of similar x values. What I mean is I need the avg of Y for each group of x. I also need to save those mean Y values.
x=[3 3 3 4 4 5 5 5 5 3 11]; y=[1 2 4 5 7 1 1 8 10 10 1];
0 comentarios
Respuesta aceptada
Star Strider
el 25 de Abr. de 2015
Editada: Star Strider
el 25 de Abr. de 2015
This looks like an application for accumarray:
x=[3 3 3 4 4 5 5 5 5 3 11];
y=[1 2 4 5 7 1 1 8 10 10 1];
y_means = accumarray(x', y, [], @mean);
EDIT:
To get a summary of the x-values (first column) and the corresponding y_means values (second column:
xu = unique(x);
ys_means = [xu' y_means(xu)]
produces:
ys_means =
3 4.25
4 6
5 5
11 1
6 comentarios
Star Strider
el 25 de Abr. de 2015
I may not fully understand exactly what you want.
There is an Answer to your other, somewhat revised and updated version of this Question, that appears to be more efficient than my revised code, but it is not obvious to me how he would also supply the corresponding indices. Before delving into this myself, I’ll see what he comes up with.
Meanwhile, I will think about this and consider an entirely new effort and a new approach in light of your new requirements.
I do appreciate your already having Accepted my Answer.
Mohammad Abouali
el 25 de Abr. de 2015
Well, updated the answer, to include both start and end index of the block, as well as x-new.
As star strider mentioned, my answer on the other post is solving the same problem but using a different approach.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!