mapping numbers to another number
70 次查看(过去 30 天)
显示 更早的评论
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
2 个评论
Jan
2021-2-4
编辑:Jan
2021-2-4
In your code the variable full_file_name is not defined.
But this is a new question, so post it in a new thread instead of the section for comments to another question.
It is recommended not to publish your email address in this forum, because this will increase the number of spam mails you get.
回答(5 个)
David Young
2015-2-19
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 个评论
Billy St. John
2020-2-6
编辑:Billy St. John
2020-2-7
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 个评论
Walter Roberson
2020-2-7
changem() appears to be from the Mapping Toolbox, which is one of the less common ones to have.
Chandrasekhar
2015-2-19
编辑:Chandrasekhar
2015-2-19
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 个评论
David Young
2015-2-19
There's nothing in the question to suggest that interpolation is needed. Indexing - much simpler and more efficient - suffices.
Michael Haderlein
2015-2-19
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 个评论
Walter Roberson
2020-2-7
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 个评论
Walter Roberson
2020-2-7
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!