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

8 Ansichten (letzte 30 Tage)
Robert
Robert am 16 Jun. 2015
Beantwortet: Walter Roberson am 17 Jun. 2015
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
  1 Kommentar
James Tursa
James Tursa am 16 Jun. 2015
Please post a short example showing the input data set and the result you are looking for.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 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!

Translated by