How to compare column A and B and give corresponding output for input provided

1 Ansicht (letzte 30 Tage)
There are 2 columns
1 - (MH12GJ8059, CH15HJ5456, MH14KS6516, KA45HS1654)
2 - (XXYYXX, GSGGYY, YSHHSG, HHSHHH)
now is there a way in Matlab so that it asks for user inputs and displays the respective data next to it.
eg, input = MH12GJ8059 (from user)
output = XXYYXX

Antworten (2)

Adam
Adam am 27 Feb. 2017
doc ismember
should do the job, using the 2nd output argument to give you the index to apply to the other array/column/whatever.

Vandana Rajan
Vandana Rajan am 28 Feb. 2017
Hi,
You may use the following code. Last 3 lines may be put inside a loop for repetition.
x = string(['MH12GJ8059'; 'CH15HJ5456'; 'MH14KS6516'; 'KA45HS1654']);
y = string(['XXYYXX'; 'GSGGYY'; 'YSHHSG'; 'HHSHHH']);
inp = input('Enter a string');
[val,ind] = ismember(string(inp),x);
disp(y(ind));
As Adam has mentioned, you can make use of the MATLAB documentation to gain more insight into the functions.
  2 Kommentare
Vandana Rajan
Vandana Rajan am 7 Apr. 2017
Yeah. No need of string command there.
x = {'MH12GJ8059'; 'CH15HJ5456'; 'MH14KS6516'; 'KA45HS1654'};
y = {'XXYYXX'; 'GSGGYY'; 'YSHHSG'; 'HHSHHH'};
inp = input('Enter a string');
[val,ind] = ismember(string(inp),x);
disp(y{ind});

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Fluid Dynamics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by