MATLAB: Rounding set of number to closest number in array

array roundMATLAB

This is the data I need to round up to. Basically, each number from x_n I need them to get rounded up to closest number in yaxis array.
Y = round(x_n, yaxis) I have tried using this command but it says "The second input must be a real integer scalar." Then I have tried making a for loop putting each data separately, but I am struggling on what command to use to round it up to closest number from an array.
yaxis = [-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.60 0.8 1]
x_n = 0 0.5878 0.9511 0.9511 0.5878 0.0000 -0.5878 -0.9511 -0.9511 -0.5878 -0.0000
0.5878 0.9511 0.9511 0.5878 0.0000 -0.5878 -0.9511 -0.9511 -0.5878
Thank you for the help.

Best Answer

Following shows a general approach
yaxis = [-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.60 0.8 1];
x = [0 0.5878 0.9511 0.9511 0.5878 0.0000 -0.5878 -0.9511 -0.9511 -0.5878 -0.0000 0.5878 0.9511 0.9511 0.5878 0.0000 -0.5878 -0.9511 -0.9511 -0.5878];
[~, idx] = min(abs(x-yaxis.'));
x_rounded = yaxis(idx)
Since yaxis vector is regular, there might be a more efficient approach.