# How to group different values in an array?

3 ビュー (過去 30 日間)
Struggling in MATLAB 2022 年 8 月 16 日
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?

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

### 採用された回答

Kevin Holly 2022 年 8 月 16 日

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:
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'
##### 1 件のコメント表示非表示 なし
Struggling in MATLAB 2022 年 8 月 16 日
Thank you!

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

### その他の回答 (1 件)

dpb 2022 年 8 月 16 日
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
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.

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

### カテゴリ

Find more on Tables in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by