Counting the number of values using hiscounts with 2 arrays
5 views (last 30 days)
Show older comments
iceskating911
on 29 Jul 2022
Edited: Cris LaPierre
on 29 Jul 2022
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1082475/image.png)
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
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);
???
Accepted Answer
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)'
3 Comments
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)
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
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!