How to group different values in an array?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a test column array which contains 4 unique values? I have attached the array.
intensityofcostcomposition
__________________________
"10.0,64.0,NaN"
"15.0,160.0,290.0"
"15.0,160.0,320.0"
"15.0,40.0,290.0"
I want to create a new table column of same length as 'composition' array which will contain 3 different unique entries ('type1','type2','type3') . I want to group of "15.0,160.0,290.0" and "15.0,160.0,320.0" as 'type2'. How do I do it?
0 commentaires
Réponse acceptée
Kevin Holly
le 16 Août 2022
Modifié(e) : Kevin Holly
le 16 Août 2022
t = table;
t.intensityofcostcomposition = ["10.0,64.0,NaN";
"15.0,160.0,290.0";
"15.0,160.0,320.0";
"15.0,40.0,290.0"]
t.NewTableColumn = ['type1';'type2';'type2';'type3']
Edit: with the attached .mat file:
load('composition.mat', 'ans')
t = ans;
t.new = t.intensityofcostcomposition
t.new(t.intensityofcostcomposition=="10.0,64.0,NaN")='type1'
t.new(t.intensityofcostcomposition=="15.0,40.0,290.0")='type3'
t.new(t.intensityofcostcomposition=="15.0,160.0,290.0")='type2'
t.new(t.intensityofcostcomposition=="15.0,160.0,320.0")='type2'
Plus de réponses (1)
dpb
le 16 Août 2022
Why are you holding numeric data as strings??? Even if stuff goes together somehow, use an array although it would seem likely given the variable name that the various components have to do with some particular measure of the cost and would probably be better off as variables reflecting such.
tT=array2table(str2double(split(C,',')),'VariableNames',{'C1','C2','C3'}); % turn into variables
[g,id1,id2]=findgroups(tT.C1,tT.C2); % get group by first two components
ug=unique(g); % unique groups/types
tT.Class=categorical(g,ug,compose('Type%d',ug-1)); % add class type
Above yields
>> tT
tT =
4×4 table
C1 C2 C3 Class
__ ___ ___ _____
10 64 NaN Type0
15 160 290 Type2
15 160 320 Type2
15 40 290 Type1
>>
where I went ahead and aribtrarily also assigned a Class ID to the other elements a well; one can always simply turn those into <ismissing> or create another categorical value for them; whatever fits the problem.
To do something similar with the data still held as strings would require string-matching for the specific pattern; gets painful very quickly that way.
You need to be able to define the rules by which the Type code is to be assigned -- the rule inferred from the description of the desired output above is that the first two C are to be the same -- that works here but might not in general, depending upon just what the logic is that decides those are, indeed, Type 2.
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!