How to have individual numbers combine into a larger number

68 vues (au cours des 30 derniers jours)
Methat
Methat le 17 Avr 2024 à 11:15
Réponse apportée : Voss le 17 Avr 2024 à 14:15
So I need to write some code that can combine multiple numbers together example so input1 = 1, input2 = 4 those 2 numbers should then = 14 instead of 5 if that makes sense. Basically I need to combine those numbers instead of adding them.
fprintf('Number of bands (4 or 5)');
disp("Select the colours from below, Enter using number:")
disp('0. Black')
disp('1. Brown')
disp('2. Red')
disp('3. Orange')
disp('4. Yellow')
disp('5. Green')
disp('6. Blue')
disp('7. Violet')
disp('8. Grey')
disp('9. White')
disp('10. Gold')
disp('11. Silver')
Bandtype = input('Please select number of bands (4 or 5): '); %Takes user input
switch Bandtype
case 4
disp('Placeholder')
Didget1 = input('Please select colour of first band (0 - 9):');
Didget2 = input('Please select colour of second band (0 - 9):');
Multiplier = input('Please select the colour of the third band (0-7, 10, 11):');
Tolerence = input('Please select the colour of the fourth band (1-2, 10-11):');
case 5
disp('Placeholder')
Didget1 = input('Please select colour of first band (0 - 9):');
Didget2 = input('Please select colour of second band (0 - 9):');
Didget3 = input('Please select colour of third band (0 - 9):');
Multiplier = input('Please select the colour of the fourth band (0-7, 10, 11):');
Tolerence = input('Please select the colour of the fifth band (1-2, 10-11):');
end
switch Didget1
case 0
Black = 0;
case 1
Brown = 1;
case 2
Red = 2;
case 3
Orange = 3;
case 4
Yellow = 4;
case 5
Green = 5;
case 6
Blue = 6;
case 7
Violet = 7;
case 8
Grey = 8;
case 9
White = 9;
end
switch Didget2
case 0
Black = 0;
case 1
Brown = 1;
case 2
Red = 2;
case 3
Orange = 3;
case 4
Yellow = 4;
case 5
Green = 5;
case 6
Blue = 6;
case 7
Violet = 7;
case 8
Grey = 8;
case 9
White = 9;
end
switch Didget3
case 0
Black = 0;
case 1
Brown = 1;
case 2
Red = 2;
case 3
Orange = 3;
case 4
Yellow = 4;
case 5
Green = 5;
case 6
Blue = 6;
case 7
Violet = 7;
case 8
Grey = 8;
case 9
White = 9;
end
switch Multiplier
end
switch Tolerence
end
if Bandtype < 4 && Bandtype > 5
disp('Invalid input please select 4 or 5!')
else
end

Réponse acceptée

Ramtej
Ramtej le 17 Avr 2024 à 11:58
Hey,
Simplest approach is to use sprintf function to format the numbers to form a single final string and convert it into double using str2double function.
inputs = [1, 4, 3, 2]; % Example array of numbers
% Concatenate using sprintf
combinedStr = sprintf('%d', inputs);
% Convert to number
combinedNumber = str2double(combinedStr);

Plus de réponses (2)

Bruno Luong
Bruno Luong le 17 Avr 2024 à 12:31
Modifié(e) : Bruno Luong le 17 Avr 2024 à 12:33
One way:
UserIput = [1 2 3 10 11];
%% Coding
X = polyval(UserIput, 16);
X % Combine number
X = 74667
clear UserIput % forget the previous data
%% Decoding: Do the revers compute UserInput from X
UserIput = dec2hex(X)-'0';
A = 'A'-'0';
Age = UserIput >= A;
UserIput(Age) = UserIput(Age)-A+10;
% The result
UserIput
UserIput = 1x5
1 2 3 10 11
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Voss
Voss le 17 Avr 2024 à 14:15
One option is to use the 's' second input to input, which signifies that the user input is returned as a character vector. Then you can simply horizontally concatenate the inputs together. If you store those inputs in a cell array, that concatenation is easy to do. Here's an example:
fprintf('Number of bands (4 or 5)');
disp("Select the colours from below, Enter using number:")
disp('0. Black')
disp('1. Brown')
disp('2. Red')
disp('3. Orange')
disp('4. Yellow')
disp('5. Green')
disp('6. Blue')
disp('7. Violet')
disp('8. Grey')
disp('9. White')
disp('10. Gold')
disp('11. Silver')
while true % loop until valid Bandtype is given
Bandtype = input('Please select number of bands (4 or 5): '); %Takes user input
if isequal(Bandtype,4) || isequal(Bandtype,5)
% if 4 or 5 is given, exit the loop
break
end
disp('Invalid input please select 4 or 5!')
end
Ordinals = {'first','second','third','fourth','fifth'};
NDidgets = Bandtype-2; % first Bandtype-2 numbers are digits, i.e., Bandtype==4 -> 2 digits; Bandtype==5 -> 3 digits
Didgets = cell(1,NDidgets); % initialize a cell array of the right size
for ii = 1:NDidgets % for loop to get the digits
Didgets{ii} = input(sprintf('Please select colour of %s band (0 - 9): ',Ordinals{ii}),'s');
end
% horizontally concatenate the digits
result = [Didgets{:}];
Multiplier = input(sprintf('Please select the colour of the %s band (0-7, 10, 11): ',Ordinals{NDidgets+1}));
Tolerence = input(sprintf('Please select the colour of the %s band (1-2, 10-11): ',Ordinals{NDidgets+2}));

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by