Counting the number of values using hiscounts with 2 arrays

3 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
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
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!