1

I have a numerical array CentroidBins which is 3694x4. Columns 3 and 4 are arbitrary X and Y bins with a range of 1-20. My goal in the last bit of code was to go through columns 3 and 4 to count the number of times a particular pair appeared (ie. 1,1 or 1,2....etc) and place that into a 20x20 array with rows being Y bins and columns being X bins. I managed to construct something which looks like what a want, but the output is 18x17, I am assuming it is deleting rows and columns populated by "0". How can I make sure this produces 20x20?

bin20 = centroids_array / 20 %create 20 bins
imRound = round(bin20)
CentroidBins = [centroids_array , imRound]
save("CentroidBins.mat", "CentroidBins");
disp(CentroidBins)

nrow = size(CentroidBins, 1); 
B = CentroidBins(:,[3 4]); 
NumF = full(sparse(B(1:end-nrow),B(nrow+1:end),1))
3
  • how is this question different from this one? stackoverflow.com/questions/61289331/… Commented Apr 18, 2020 at 16:55
  • Hey @FangQ , thanks for the help on that one. It was summing the values of column 1, whereas this one is summing the number of occurrences of each pair in 3/4. Should it be approached the same way? Commented Apr 18, 2020 at 16:58
  • see my answer below Commented Apr 18, 2020 at 17:02

1 Answer 1

2

to count the occurrence of pairs, you use hist and unique

a=[1 2; 1 2; 2 3; 8 1; 2 3];
[foo,ix,jx]=unique(a,'rows');
count=hist(jx,unique(jx)) % report the repeated counts of each unique pair
foo                       % lists the unique pairs
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.