Merging two categorical columns using a rule
Afficher commentaires plus anciens
Hi,
I am wanting to merge/add 2 categorical columns but need to use a rule.
For example a= [Flowering not not not], b=[not not not budburst] and I want the final output to be c=[Flowering not not Budburst]?
Basically, when merging/adding the two vectors, I want the word "flowering" or "budburst" to take precedence over "not". "Flowering" will never clash with "budburst" and "not "not" would give a "not".
Thanks, Wendy
Réponse acceptée
Plus de réponses (3)
Greg
le 11 Mai 2018
0 votes
Make your categorical array ordinal (with "not" as the lowest value), then use c = max(a,b). This is only guaranteed to work since you said valid values won't conflict.
3 commentaires
Wendy Cameron
le 11 Mai 2018
Greg
le 12 Mai 2018
Basically, you specify the valueset (allowable entries) in order of smallest to largest:
valueset = {'not';'Budburst';'Flowering'};
var = categorical(a,valueset,'ordinal');
Wendy Cameron
le 12 Mai 2018
Wendy Cameron
le 11 Mai 2018
0 votes
1 commentaire
Ameer Hamza
le 11 Mai 2018
I just tested my code with your data and it is giving correct output. Can you tell which line is creating the error? Refer to my edited answer to see how to read data from the xls file.
Wendy Cameron
le 11 Mai 2018
0 votes
3 commentaires
Ameer Hamza
le 11 Mai 2018
You can add the new column to the original table using,
data.newColumnName = c;
where data is the table, newColumnName is the name you want to give to the new column and c is the result.
Wendy Cameron
le 11 Mai 2018
Ameer Hamza
le 11 Mai 2018
You are welcome.
Catégories
En savoir plus sur Tables dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!