Using a matrix as a index to perform functions on another - MATLAB -


i have 2 matrices in form:

ind= 1 1 1 1 2 2 2 2 2 3 3  type =  a b a b b b 

i want able identify pairs of specific kind i.e. a-b , b-a not a-a. have been able using if statements in form:

if strcmp(type(m),'a') == 1 && strcmp(type(m+1),'b') == 1 && ind(m) == ind(m+1) 

and forth.

as hinted within if statement, need able calculate how many valid pairs there per index.

for example, first 4 types aaba belong index '1' because index '1' has length of 4 specified in ind. here there 2 valid pairs a-b , b-a. a-a not valid pair.

the desired output full above example be:

2 4 1 

is there quick , easy way accomplish this?

edit:

if types expanded include 'c' - , system needs detect non-unique pairs i.e. a-b, b-a b-b (but nothing containing c) - done? there way specify pairs being counted each time?

you can try:

ind = [1 1 1 1 2 2 2 2 2 3 3]';   type = 'aabaabababa'; accumarray(ind(intersect([strfind(type,'ab'),strfind(type,'ba')],find(~diff(ind)))),1) 

output:

ans =       2      4      1 

if recall correctly, arrayfun kind of slow. don't think vectorizes code. anyway, idea find 'ab' , 'ba' strfind , merge indices together. however, cannot count 'ab' , 'ba' across index boundaries, intersect find(~diff(ind)) make sure valid indices kept. accumarray accumulate indices ind answer want.


Comments