How can I bin Data

My problem is the following:
How can I bin the data in this case: I have a matrix with column 1 x with 90 values of price and column 2 y with 90 values of demand. I want to bin the x values for example
x<30 as x1, 30<x<35 as x2,35<x<40 as x3, 40<x<45 as x4,45<x<47 as x5,47<x49 as x6, 49<x<52 as x7 and x>52 as x8. And I want to have for each x1,x2,..,x8 all the replicates of y in a matrix.
Then to find the mean of y-s for each x1,x2,...,x8.

Respuestas (2)

Star Strider
Star Strider el 14 de Mzo. de 2019

0 votos

There are probalby several ways.
One approach:
x = randi(90, 90, 1); % Create Data
y = rand(90,1) .* sin(2*pi*x/90); % Create Data
partitions = [0 30; 30 35; 35 40; 40 45; 45 47; 47 49; 49 52; 52 max(x)+1];
for k1 = 1:size(partitions,1)
ypartmean(k1) = mean(y((x >= partitions(k1,1)) & (x < partitions(k1,2))));
end
Experiment to get the result you want.

7 comentarios

gjashta
gjashta el 14 de Mzo. de 2019
I need also to include the histogram bin counts.
Star Strider
Star Strider el 14 de Mzo. de 2019
Add one assignment:
for k1 = 1:size(partitions,1)
ypartmean(k1) = mean(y((x >= partitions(k1,1)) & (x < partitions(k1,2))));
binct(k1) = numel(y((x >= partitions(k1,1)) & (x < partitions(k1,2))));
end
The ‘binct’ vector will have the bin counts.
gjashta
gjashta el 14 de Mzo. de 2019
Thank you! How can I see the histogram?
gjashta
gjashta el 14 de Mzo. de 2019
Attached is how I am trying to bin the data.
Star Strider
Star Strider el 14 de Mzo. de 2019
My pleasure.
Try this:
for k1 = 1:size(partitions,1)
ypartmean(k1) = mean(y((x >= partitions(k1,1)) & (x < partitions(k1,2))));
binct(k1) = numel(y((x >= partitions(k1,1)) & (x < partitions(k1,2))));
end
ctrs = median(partitions,2);
figure
hb = bar(ctrs, binct);
xt = get(gca,'XTick');
xtl = sprintfc('%d-%d', partitions)';
set(gca, 'XTick',ctrs, 'XTickLabel',xtl', 'XTickLabelRotation',45)
This would have been much easier if you had provided your data, and mentioned all these additional requirements in your original post.
gjashta
gjashta el 14 de Mzo. de 2019
Thanks a lot Star Strider! Sorry for confusing you!
Star Strider
Star Strider el 14 de Mzo. de 2019
No worries, and no confusion. It simply would have been easier to have addressed all this at once.
If my Answer helped you solve your problem, please Accept it!

Iniciar sesión para comentar.

Steven Lord
Steven Lord el 14 de Mzo. de 2019

0 votos

If I understand your application correctly use histcounts2 to bin the data. The fourth and fifth output are the indices of the bins into which each element of your x and y data is binned. Use that bin information as the grouping variables in a call to groupsummary. See the "Multiple Grouping Vectors for Vector Input" example on the groupsummary documentation page for an illustration of the latter part of this technique.

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Mzo. de 2019

Comentada:

el 14 de Mzo. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by