Replace values in matrix with values based on look up table, without loop?

8 views (last 30 days)
I'm new to this type of data manipulation, I have a large matrix with a set of values which I would like to convert to different values based on a table. I did a search for lookup tables, but did not find functions readily, is this something that requires one of the tool boxes? Thanks!
Talbot

Answers (1)

Walter Roberson
Walter Roberson on 17 Jun 2015
mapfrom = [2.8 19 -3 pi 42 1];
mapto = [5 6 2 41 -pi 7];
%we need to get them in sorted order
[sortfrom, sortidx] = sort(mapfrom);
sortto = mapto(sortidx);
Now to look up,
MappedValues = interp1(sortfrom, sortto, ValuesToLooKUp, 'nearest');
The 'nearest' is not needed if all of the original values and all of the values to look up are integers, but if any of them are floating point then you need it because floating point values often differ in the last bit or two from what you think they are.

Community Treasure Hunt

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

Start Hunting!