Comparing two tables and replacing categorical column, row wise.
2 views (last 30 days)
Show older comments
Hello ,
I have two tables named Xfeat and Xfeat1, each table is having two coloumns 'feat_mat' and 'Label' . In label column there are 2 values 'Normal' and 'Hypopnea'
I need to compare Label coloumn from both the tables
Here's the code:
Xfeat.Label = categorical(Xfeat.Label);
Xfeat1.Label = categorical(Xfeat1.Label);
[a,a1]=size(Xfeat);
[b,b1]=size(Xfeat1);
c=min(a,b);
for i=1:c
Xfeat2 = table;
if Xfeat(Xfeat.Label(i) == 'Normal')&&Xfeat1(Xfeat1.Label(i) == 'Normal')
Xfeat2(i,:) = [Xfeat2 ; cell2table({'Normal'})];
else Xfeat2(i,:) = [Xfeat2 ; cell2table({'Hypopnea'})];
end
end
This code is giving me this error:
(line 8)
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript
and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one variable
t.(i). To select rows, use t(i,:).
Here is a screenshot that might help
0 Comments
Answers (2)
VBBV
on 22 Jun 2023
Edited: VBBV
on 22 Jun 2023
Here's one way to compare Label columns from two tables.
Do you also want to add the corresponding data from two tables to the final table ?
feat_mat = rand(10,6);
Label1 = ["Hypopnea";"Normal";"Normal";"Hypopnea";"Hypopnea";"Normal";"Normal";"Normal";"Normal";"Normal"];
Label2 = ["Normal";"Hypopnea";"Hypopnea";"Hypopnea";"Hypopnea";"Normal";"Normal";"Normal";"Hypopnea";"Hypopnea"];
Xfeat = table(feat_mat,Label1)
feat_mat1 = rand(10,6);
Xfeat1 = table(feat_mat1,Label2)
Xfeat.Label1 = categorical(Xfeat.Label1);
Xfeat1.Label2 = categorical(Xfeat1.Label2);
[a,a1]=size(Xfeat);
[b,b1]=size(Xfeat1);
c=min(a,b);
for i=1:c
if Xfeat.Label1(i) == "Normal" & Xfeat1.Label2(i) == "Normal"
Xfeat2(i,:) = cell2table({"Normal"});
else
Xfeat2(i,:) = cell2table({"Hypopnea"});
end
end
Xfeat2
0 Comments
Stephen23
on 22 Jun 2023
Why are you using a loop for this? MATLAB works best when you work in terms of vectors, matrices, and arrays:
T1 = array2table(rand(10,6));
T2 = array2table(rand(10,6));
T1.Label = categorical({'Hypopnea';'Normal';'Normal';'Hypopnea';'Hypopnea';'Normal';'Normal';'Normal';'Normal';'Normal'})
T2.Label = categorical({'Normal';'Hypopnea';'Hypopnea';'Hypopnea';'Hypopnea';'Normal';'Normal';'Normal';'Hypopnea';'Hypopnea'})
C = {'Hypopnea';'Normal'};
N = min(height(T1),height(T2));
X = T1{1:N,'Label'}=="Normal" & T2{1:N,'Label'}=="Normal";
D = C(1+X)
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!