How do I replace every value in a column with a value from a lookup table?

9 views (last 30 days)
I have a column of data with 8800 values, I need to create a code that replaces each value in that column with another value based on a look up table. Lets say column A = randi(50000,8800,1). The lookup table is as followed...
X Y
8000 25
15850 32.5
18000 44
20250 61.2
23400 61.2
25000 65.9
31850 95
32850 95
50000 95
If the value of column A = 8000 it should be replaced with 25, if A = 23400 then it should be replaced with 61.2 and so on. However, if A = a value between the lookup table values it should interpolate for a value. A linear relationship between the upper and lower value is fine. Ex: If A = 11925 (halfway between 8000 & 15850) it should be replaced with 28.75.
I was thinking a for loop with if statements, but I haven't used Matlab in a few years and am a little rusty. Appreciate any help.

Accepted Answer

David Hill
David Hill on 9 Jun 2022
interp1(X,Y,A)

More Answers (1)

Image Analyst
Image Analyst on 10 Jun 2022
Edited: Image Analyst on 10 Jun 2022
Try my well commented, illustrative example:
% Define our look up table.
xyLut = [...
8000 25
15850 32.5
18000 44
20250 61.2
23400 61.2
25000 65.9
31850 95
32850 95
50000 95];
% Make sure it's sorted by the first column
xyLut = sortrows(xyLut, 1);
% Plot it so we can visualize output vs input.
plot(xyLut(:, 1), xyLut(:, 2), 'b.-', 'LineWidth', 2, 'MarkerSize', 30)
grid on;
xlabel('Input Value')
ylabel('outputValue')
% Make up some array of 7 data points for input
% that lies in between the min and max of the look up table.
inputValues = linspace(min(xyLut(:,1))+1000, max(xyLut(:,1))-1000, 7)
inputValues = 1×7
1.0e+04 * 0.9000 1.5667 2.2333 2.9000 3.5667 4.2333 4.9000
% Interpolate the output values according to the look up table.
outputValues = interp1(xyLut(:, 1), xyLut(:, 2), inputValues)
outputValues = 1×7
25.9554 32.3248 61.2000 82.8927 95.0000 95.0000 95.0000
% Plot where they fall on the look up table curve.
hold on
plot(inputValues, outputValues, 'r.', 'MarkerSize', 30)
legend('Look up table', 'Reassigned Data', 'Location', 'northwest')

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!