How to group different values in an array?

8 views (last 30 days)
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?

Accepted Answer

Kevin Holly
Kevin Holly on 16 Aug 2022
Edited: Kevin Holly on 16 Aug 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 = 4×1 table
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']
t = 4×2 table
intensityofcostcomposition NewTableColumn __________________________ ______________ "10.0,64.0,NaN" type1 "15.0,160.0,290.0" type2 "15.0,160.0,320.0" type2 "15.0,40.0,290.0" 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'

More Answers (1)

dpb
dpb on 16 Aug 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.

Tags

Products

Community Treasure Hunt

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

Start Hunting!