Counting the number of values using hiscounts with 2 arrays

9 views (last 30 days)
Hello! I have a question about using hiscounts to count the number of values in an array. To give some background, I currently have two arrays that look like this:
A (left) is a 21x1 double and B (right) is a 21x 1 double. I want to take values in array A and bin them in octive spacing bins (2-4, 4-8, 8-12) etc while also taking the corresponding values in bin B with them. Then for a particular bin, ex: 2-4, I want to count how many values of array B fall in that certain bin size. So I want my final result (List_of_values) to be a 16x1 array that has counted each of the values in array B that falls into it's corresponding size in array A. Hopefully this is clear enough so far, I'm new at coding. Here is my code so far
edges = 2.^(-2:14); %This defines our bins with octave spacing
a = Length_1;
b = Weight_1;
for k = 1 : length(edges)-1
indexes = a > edges(k) & a <= edges(k+1);
List_of_values(k) = histcounts(b(indexes));
end
But, I keep running into errors like "Unable to perform assignment because the left and right sides have a different number of elements." which makes sense because b is a 21 x 1 array and indexes is creating a 1 x 16 array.
Please help!! I'm not sure where to go from here.
  2 Comments
dpb
dpb on 29 Jul 2022
"I want to take values in array A and bin them in octive spacing bins (2-4, 4-8, 8-12) etc while also taking the corresponding values in bin B with them. Then for a particular bin, ex: 2-4, I want to count how many values of array B fall in that certain bin size..."
Why isn't this just
N=histcounts(B,edges);
???
iceskating911
iceskating911 on 29 Jul 2022
It is! I just was overthinking it a bit too much so thanks for the clarity!

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 29 Jul 2022
It is unclear to me exactly what you are trying to do, and why you need to have A to know the bincounts of B. All you need is the edges. With that, you can get a count of how many values of B fall into each bin without using a for loop.
edges = 2.^(-2:14);
B=rand(21,1)*100;
List_of_values = histcounts(B,edges)'
List_of_values = 16×1
0 0 1 0 3 2 0 9 6 0
  3 Comments
Cris LaPierre
Cris LaPierre on 29 Jul 2022
Edited: Cris LaPierre on 29 Jul 2022
Looking at your code, perhaps you meant you only wanted to count the rows of B corresponding to the values of A that are in a specific bin? For example, if I bin vector A and look at the results in the first bin, I would then want to look at the corresponding rows in B, and then find how many of those values would also fall into bin 1. Is that correct?
If so, use the optional outputs of histcounts to make things simpler. The third output is the bin number each of the values of A fell into. Since A and B are the same size, it can be used to identify the values in B that correspond to the values of A in a given bin.
The error you are getting is because histcounts returns a vector. You want a single value. Use a loop to assign just the count for the current bin to List_of_values.
edges = 2.^(-2:14);
A=rand(21,1)*100;
[N_A,~,Abins] = histcounts(A,edges)
N_A = 1×16
0 0 0 0 0 1 3 5 12 0 0 0 0 0 0 0
Abins = 21×1
9 9 8 8 8 7 9 9 9 7
B=rand(21,1)*100;
List_of_values = zeros(length(edges)-1,1);
for b = 1:max(Abins)
N = histcounts(B(Abins==b),edges)';
List_of_values(b) = N(b);
end
List_of_values
List_of_values = 16×1
0 0 0 0 0 0 0 1 3 0
iceskating911
iceskating911 on 29 Jul 2022
Wow thanks for the detailed reponse! I think I was just overthinking the process, counting whatever falls into a certain bin for A, whether its A or B, should still create the same answer. I'm not sure what I was really trying to visualize but your quesiton helped me see i was overthinking it. thank you again

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!