Can I create something like histogram2d using filter in Julia

i learned it is possible to create histogram like chart by using the Julia code

using Plots
a = randn(1000);
b = [length(filter(x -> i <= x < i+.2, a)) for i = -4:.2:4];
bar(b);

i know this code is no good in terms of performance. but i expected the filter method to be able to deal with two arrays, so that i can write something like

a = randn(100); b = randn(100);
c = [length(filter((x,y) -> i<=x<i+.2 && j<=y<j+.2 , a, b)) for i = -4:.2:4, j = -4:.2:4];

could you please help me find some short way of creating an array according to histogram2d rules?

this is my very first coding question on internet. sorry for anything weird.

A different approach is for you to turn your input into a vector of tuples. See below. This isn’t necessarily the best way to approach this problem, but it is a direct answer to your question.

AB = collect(Iterators.zip(a,b))
c = [length(filter(z -> i<=z[1]<i+0.2 && j<=z[2]<j+0.2 , AB)) for i = -4:.2:4, j = -4:.2:4]

Also, please quote your code using backticks.

2 Likes

Thank you!