# Creating a new column in a table which is true or false

6 vues (au cours des 30 derniers jours)
Luca le 6 Mai 2021
Commenté : Luca le 9 Mai 2021
Hi,
I have to tables. One is called subsetx the other one is called subsety. In subsetx are two columns one is called ID and the other one SIC. It looks like this:
ID SIC
10000 3990
10000 3990
.
.
.
10001 4920
.
.
.
10002 6020
In subsety i have two rows with value 3990 4920.
I would like to create a new column in subsetx of logical type (true or false)
If variable SIC matches one of the numbers in subsety I would like to get a 1 if not i would like to get a 0.
The proble is you cant compare table with diffrent numbers of row.
I did attached the two subsets.
How can I solve this problem ?
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Steven Lord le 6 Mai 2021
rng default
id = (1:10).';
SIC = randi(3, 10, 1);
t = table(id, SIC)
t = 10×2 table
id SIC __ ___ 1 3 2 3 3 1 4 3 5 2 6 1 7 1 8 2 9 3 10 3
t.is1or2 = ismember(t.SIC, [1 2])
t = 10×3 table
id SIC is1or2 __ ___ ______ 1 3 false 2 3 false 3 1 true 4 3 false 5 2 true 6 1 true 7 1 true 8 2 true 9 3 false 10 3 false
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Luca le 9 Mai 2021
Thank you very much it worked.

Connectez-vous pour commenter.

### Plus de réponses (1)

David Fletcher le 6 Mai 2021
Modifié(e) : David Fletcher le 6 Mai 2021
vals=subsety{:,1};
subsetx{:,3}=subsetx.SIC==vals(2,1)|subsetx.SIC==vals(1,1);
##### 1 commentaireAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens
Luca le 9 Mai 2021
Thank you it worked

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Application Deployment dans Help Center et File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by