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

1 回表示 (過去 30 日間)
Nishant Satav
Nishant Satav 2017 年 2 月 27 日
コメント済み: Vandana Rajan 2017 年 4 月 7 日
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

回答 (2 件)

Adam
Adam 2017 年 2 月 27 日
doc ismember
should do the job, using the 2nd output argument to give you the index to apply to the other array/column/whatever.
  1 件のコメント
Nishant Satav
Nishant Satav 2017 年 2 月 28 日
but what is the syntax? could you give me a working code for the above example?

サインインしてコメントする。


Vandana Rajan
Vandana Rajan 2017 年 2 月 28 日
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 件のコメント
Nishant Satav
Nishant Satav 2017 年 4 月 1 日
this code has errors.
Vandana Rajan
Vandana Rajan 2017 年 4 月 7 日
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});

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDownloads についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by