MATLAB: Counting specific element in one column corresponding to unique elements in an another column

accumarrayhistcount

I have two vectors a and b. I want to count how many times the value '-1' appears in b corresponding to unique elements in a. Is there an efficient way to do this without loops?
a = [1,1,1,1,1,2,2,2,2,2,3,3,4,4,4,5,5,5,5,5,5]';
b = [1,1,-1,1,-1,-1,-1,-1,1,1,-1,1,1,-1,-1,1,1,-1,-1,1,1]';
desired output:
1--2
2--3
3--1
4--2
5--2

Best Answer

You can use logic indexing. Example:
length(b(a==1 & b==-1));
You get those entries from b which fulfils the condition that the corresponding index position in a must equal 1, and also that the same index position in b must equal -1.
& is logic AND - both conditions must be true to get an output. If you need a logic OR, the matlab operator is |