MATLAB: Comparing 2 matrixes A and B and reassigning values in 2nd matrix B based on the first matrix A

comparison

I want to compare two matrixes and produce result as follows, if A=[5 10 15 20 22 24 26 30 34] in A first 4 element increased by 5 till 20 and next element from 20 to 26 increased by 2 and the rest is increased by 4 when only the incremants changes compare to previous I need to recalculate it by making it orgin. B=[1 1.5 2 4 5 8 14 17 25 18 16 22 23 20 27 29 32 34]; and the recalculation has to be happened only during the differnce between the element changes at A matrix. answer C=[1 1.5 2 4 5 8 14 17 3 18 16 2 3 17 1 20 1 2 6 8]. could it be compared and done. I tried some ways but it is not working as I expected

Best Answer

Your description still leaves things unclear. The fact that you don't make sure that your examples are correct does not help. At one point you say that C=[1 3 2 4 5 2 ...] then you say C=[1 3 2 4 5 8 ...].
I still don't know why the 16 gets changed to 6, nor why the final 29 gets changed to 12. Perhaps, it's a typo, but when describing some obscure algorithm you should ensure your examples are correct.
Possibly, this will do what you want:
A = [ 2 4 6 10 14 18 21 24 27 30];
B = [1 3 2 4 5 8 14 17 25 18 16 22 23 20 27 29];
thresholds = A([false, diff(diff(A)) ~= 0]); %find values of A where increment jumps
offsets = [0, thresholds]; %create offsets for before 1st jump and after each jump
offsetidx = discretize(B, [-Inf, thresholds, +Inf]); %find which offset each element of B corresponds to
C = B - offsets(offsetidx)
which gives:
C = [1 3 2 4 5 2 8 11 7 0 10 4 5 2 9 11]
The 16 gets changed to 10 since it between 6 and 18, where the offset is 6. 29 gets changed to 11 since the offset is 18.