MATLAB: Find max of a vector between two indice

maxmax between two indicemin

Hi, I want a code to find the maximum number between two indice of a vector.
I have an indice vector and a data vector . For example my indice vector is :
A=[3 7 13]
and my data vector is:
B=[-5 3 -8 1 -7 2 -9 3 6 2 7 9 -4 2 6]
now the max number between indices 3 and 7 of vector B is number "2" .
and the max number between indices 7 and 13 of vector B is number "9".
So the output should be like a vector named C as follows:
C=[2 9]
I've been thinking on this for hours but haven't found any solution yet 🙁
Tnx in advance.

Best Answer

>> C = arrayfun(@(x) max(B(A(x):A(x+1))),1:length(A)-1)
C =
2 9