MATLAB: Round towards target matrix

round irregular user defined

Hi @ all 🙂
I have a small problem with rounding values of a base matrix towards the values of a target matrix. The matrices looks like this:
Base matrix:
# 1.12
# 1.16
# 2.0
# 1.78
# 2.3
# 3.1
# 3.67
# 1.1
# 0.3
Target matrix:
# 0
# 0.3
# 0.8
# 1
# 1.5
# 2.4
# 2.5
# 3.1
# 3.3
# 4
Now I want to round the values of the base matrix to the values of the target matrix, resulting to this "round values":
# 1.12 -> 1
# 1.16 -> 1
# 2.0 -> 2.4
# 1.78 -> 1.5
# 2.3 -> 2.4
# 3.1 -> 3.1
# 3.67 -> 4.0
# 1.1 -> 1
# 0.3 -> 0.3
In both matrices the differences/spacing between the values are irregular. Using "ceil", "floor", "round" I have no chance to solve this problem… (or only with regular spacing)
Thanks a lot for any kind of help!
Reik

Best Answer

I think maybe you want interp1() with the 'nearest' option.