Hi ,
I have two columns of numbers A and B. A is 9 digits and B is 8 digits. How can I find the row in B that matches A(i) in a loop (i=1:length(A))

3 件のコメント

Walter Roberson
Walter Roberson 2017 年 10 月 25 日
What does it mean for a row in B to match in this case?
wesso Dadoyan
wesso Dadoyan 2017 年 10 月 25 日
example A(2)=654356105 B is column vector that contains in row 20 number 65435610. B(20) matches A(2) since the first 8 digits of A(2)=B(20) how can I find row 20 in B?
Rik
Rik 2017 年 10 月 25 日
I would recommend not using a loop. If you have stored the number in a number format, just divide the A by 10 and floor it to convert to 8 digits. If you have it as a string, just remove the last character.
Then you can use ismember to do the work. ismember can be slow, so looking for alternative methods can be worthwhile if that is the bottleneck in your code.

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

 採用された回答

Walter Roberson
Walter Roberson 2017 年 10 月 25 日

0 投票

[tf, Bidx] = ismember(fix(A/10), B);
Now, tf(K) tells you whether A(K) had a match in B or not, and if tf(K) is true then Bidx(K) tells you which entry in B matched.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

タグ

質問済み:

2017 年 10 月 25 日

コメント済み:

Rik
2017 年 10 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by