How do I replace every value in a column with a value from a lookup table?
14 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Noah Kroeze
el 9 de Jun. de 2022
Editada: Image Analyst
el 10 de Jun. de 2022
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.
0 comentarios
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 10 de Jun. de 2022
Editada: Image Analyst
el 10 de Jun. de 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)
% Interpolate the output values according to the look up table.
outputValues = interp1(xyLut(:, 1), xyLut(:, 2), inputValues)
% 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')
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!