Q:

Using a matrix as a index to identify instances of characters in another - MATLAB

I have three matrices specifying an index (ind), the type of datapoint (type - character list) and the value of this datapoint (pos). 
ind=
1
1
1
1
2
2
2
2
2
3
3

type = 
A
A
B
A
C
B
C
B
A
B
C

pos = 

52150
109583
131411
193221
69719
203747
217250
269560
306121
324084
333170

Problem 1: What is the best way to tally the instances of each type within the confines of the index? e.g. there are 3A's and 1B for index = 1. The following partially works but cuts off any datapoint occurring at the boundary of different indices:
Count(:,1) = accumarray(ind(intersect([strfind(type,'A')],find(~diff(ind)))),1);

Problem 2: How could I use the same index to determine the diff between the datapoints for specific type pairs? e.g. if I wanted to find the difference between A-A pairs, ind = 1 would give the answer 109583-52150 = 57433 and 193221-109583 = 83,638. Within ind = 2 it would then give no answer as there is only 1 A in this index. These are always calculated sequentially down the matrix - one letter with it's next valid letter. 
I will sometimes need to find more than one pair e.g. A-B and B-A distances - skipping any C's. 
EDIT: When the pairs are mixed i.e. A-B/B-A, diff is calculated between the current letter and whatever is the next matching letter - sequentially down the matrix. 
For ind = 1 the results would be:
109583-52150 = 57433 (A2-A1)
131411-109583 = 21,828 (B1-A2)
193221-131411 = 61,810 (A3-B1)

If a C was present and this was not being searched for it, it would simply be ignored and the value of the next matching point used for diff. 

A:

To count the occurances you could use
[v, c] = unique(type(ind==x));

That gives you each element in type (specified by ind ofc) and in c you have the number of occurances. (did I get the question right here?)
The second question I don't understand. How would a A-B diff look like if you have 4A's and 1B's?
In general, you can use the &-Operator to combine different restictions, e.g.
pos((ind==1)&(type=='A'))
ans =
       52150
      109583
      193221
diff(pos((ind==1)&(type=='A')))
ans = 
       57433
       83638

If you need to add several type's, you can simply add them by using this
pos((ind==1)&((type=='A')|(type=='B')))
ans =
       52150
      109583
      131411
      193221
diff(pos((ind==1)&((type=='A')|(type=='B'))))
ans =
       57433
       21828
       61810

Hope this helps

