MATLAB Answers

mapping numbers to another number

137 views (last 30 days)
bkshn
bkshn on 19 Feb 2015
Commented: Walter Roberson on 7 Feb 2020
Hello
I want to map numbers 1,2,3,4,5,6,7,8,9 to 11,12,13,21,22,23,31,32,33. for example if I have number '1' I get '11'
if I have number '2' I get '12' . I don't want to use 'IF statement', because time and performance is important for me. In addition to 'switch case' is there any solution?
Thanks

Answers (5)

David Young
David Young on 19 Feb 2015
I don't see why switching case might be relevant to this problem.
Put the numbers you want to map to into an array
outputNumbers = [11,12,13,21,22,23,31,32,33];
and then perform the mapping using array indexing
x = 3; % input number for testing
y = outputNumbers(x); % output number
which will assign 13 to y. This will work for multiple numbers, for example
x = [3 5 9];
y = outputNumbers(x);
which will assign [13 22 33] to y.
If you want your numbers to be represented by strings rather than as binary, use a cell array of strings rather than an array of numerical values.

  0 Comments

Sign in to comment.


Chandrasekhar
Chandrasekhar on 19 Feb 2015
Edited: Chandrasekhar on 19 Feb 2015
use
vq = interp1(x,v,xq)
where x is the first set of numbers and r is the second set of numbers.
x = [1,2,3,4,5,6,7,8,9]
v = [11,12,13,21,22,23,31,32,33]
xq is the input number

  1 Comment

David Young
David Young on 19 Feb 2015
There's nothing in the question to suggest that interpolation is needed. Indexing - much simpler and more efficient - suffices.

Sign in to comment.


Michael Haderlein
Michael Haderlein on 19 Feb 2015
If you don't want to limit it to the first 9 numbers, you can also use this equation:
>> x=1:15;
>> y=mod(x-1,3)+1+(1+fix((x-1)/3))*10;
>> y
y =
11 12 13 21 22 23 31 32 33 41 42 43 51 52 53

  0 Comments

Sign in to comment.


Billy St. John
Billy St. John on 6 Feb 2020
Edited: Billy St. John on 7 Feb 2020
While Indexing and Linear Interpolation can be used with the data in the question, for anyone else (like me) that came here trying to solve the general problem of mapping certain values in any array to a different values (E.g. substituting values in a data array), consider using changem if you have the Mapping Toolbox or implement your own function as detailed here
Ex:
dataToConvert = [1 1 2 20 22 3 55];
from = [3 55 20];
to = [3000 1234 888];
convertedChangem = changem(dataToConvert, to, from)
% convertedChangem =
% [1 1 2 888 22 3000 1234]
convertedInterp1 = interp1(from, to, dataToConvert)
convertedIndexing = to(dataToConvert)
% convertedInterp1 =
% [NaN NaN NaN 888 907.8 3000 1234]
% convertedIndexing = to(dataToConvert) raises an error with "Index exceeds matrix dimensions."
Note that Linear Interpolation results in NaNs for certain values and Basic Indexing does not work unless the values in the dataToConvert array are all valid indices of the to array.

  3 Comments

Billy St. John
Billy St. John on 7 Feb 2020
Stephen, I should have made my original example a little more robust / non-consecutive (I've since modified it). I was referring to the more general case of substituing a number (or numbers) with another number (or numbers) in an arbitary array. Hopefully that makes more sense.
Walter Roberson
Walter Roberson on 7 Feb 2020
changem() appears to be from the Mapping Toolbox, which is one of the less common ones to have.
Billy St. John
Billy St. John on 7 Feb 2020
Interesting, i didn't realize that. I'll add that note to my comment.

Sign in to comment.


Walter Roberson
Walter Roberson on 7 Feb 2020
One solution:
%this does not require that old or new be integers or positive
old = [1,2,3,4,5,6,7,8,9];
new = [11,12,13,21,22,23,31,32,33];
[found, idx] = ismember(TheNumbers, [1,2,3,4,5,6,7,8,9]);
replaced_numbers = TheNumbers;
replaced_numbers(found) = new(idx(found));
But in the case where the old series is consecutive integers:
oldmin = 1; oldmax = 9; %does not have to start at 1
new = [11,12,13,21,22,23,31,32,33];
mask = TheNumbers >= oldmin & TheNumbers <= oldmax;
replaced_numbers = TheNumbers;
replaced_numbers(mask) = new(TheNumbers(mask)-oldmin+1); %requires that old is integers
Another approach, especially if you know the range of values ahead of time and they are integers:
LUT = cast(0 : maximum_possible_input, class(TheNumbers)); %e.g., cast(0:255,'uint8')
LUT(1+(1:10)) = [11,12,13,21,22,23,31,32,33];
replaced_numbers = LUT(double(TheNumbers)+1);
This version of the code assumes that TheNumbers might be an integer data type that includes 0, such as uint8 . The double(TheNumbers) is needed because uint8(255)+1 would saturate to 255 whereas we need 255 to map to 256 because we have to shift index by 1 to allow for 0.
In the special case where the inputs are integer but never include 0:
LUT = cast(1 : maximum_possible_input, class(TheNumbers)) %e.g., cast(1:255, 'uint8')
LUT(1:10) = [11,12,13,21,22,23,31,32,33];
replaced_numbers = LUT(TheNumbers);

  1 Comment

Walter Roberson
Walter Roberson on 7 Feb 2020
If you are working with integers and you have the Image Processing Toolbox, you can also use https://www.mathworks.com/help/images/ref/intlut.html
LUT = uint8(0:255);
LUT(1+(1:10)) = [11,12,13,21,22,23,31,32,33];
replaced_numbers = imlut(TheNumbers, LUT);

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!