Comparing columns and returning 0s for matching values

2 ビュー (過去 30 日間)
Pablo Jaramillo Restrepo
Pablo Jaramillo Restrepo 2016 年 2 月 8 日
編集済み: Guillaume 2016 年 2 月 8 日
I have two columns in two separate tables:
T1 - Col A T2 - Col A
1 1
2 4
3 ` 5
4
5
I am looking to create in table 1 a column B containing zeros for those matching values between T1-colA and T2-colA.
The result will look like
T1 - Col A T1 - Col B
1 0
2
3 `
4 0
5 0
How could I accomplish this efficiently?
  1 件のコメント
Pablo Jaramillo Restrepo
Pablo Jaramillo Restrepo 2016 年 2 月 8 日
...(sorry I forgot something) and for those values not matching, return a value (FOR EXAMPLE 1) so the resulting table will look like:
T1 - Col A T1 - Col B
1 0
2 1
3 ` 1
4 0
5 0

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

採用された回答

Ingrid
Ingrid 2016 年 2 月 8 日
is it a table that you have or an array? Have you looked into ismember to see if this does what you want?
doc ismember
  2 件のコメント
Pablo Jaramillo Restrepo
Pablo Jaramillo Restrepo 2016 年 2 月 8 日
Thank you Ingrid! this function works well for what I am doing!
Guillaume
Guillaume 2016 年 2 月 8 日
編集済み: Guillaume 2016 年 2 月 8 日
Whether or not it is truly a table or just a plain array, the answer is the same. Use ismember:
%assuming tables
T1.colB = ismember(T1.colA, T2.colA);
%assuming matrices
T1(:, end+1) = ismember(T1(:, 1), T2(:, 1));

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by