Counting the number of values using hiscounts with 2 arrays

조회 수: 5 (최근 30일)
iceskating911
iceskating911 2022년 7월 29일
편집: Cris LaPierre 2022년 7월 29일
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
dpb
dpb 2022년 7월 29일
"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 2022년 7월 29일
It is! I just was overthinking it a bit too much so thanks for the clarity!

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 7월 29일
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
Cris LaPierre
Cris LaPierre 2022년 7월 29일
편집: Cris LaPierre 2022년 7월 29일
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 2022년 7월 29일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by