Comparing two tables and replacing categorical column, row wise.

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

Answers (2)

VBBV
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)
Xfeat = 10×2 table
feat_mat Label1 __________ __________ 1×6 double "Hypopnea" 1×6 double "Normal" 1×6 double "Normal" 1×6 double "Hypopnea" 1×6 double "Hypopnea" 1×6 double "Normal" 1×6 double "Normal" 1×6 double "Normal" 1×6 double "Normal" 1×6 double "Normal"
feat_mat1 = rand(10,6);
Xfeat1 = table(feat_mat1,Label2)
Xfeat1 = 10×2 table
feat_mat1 Label2 __________ __________ 1×6 double "Normal" 1×6 double "Hypopnea" 1×6 double "Hypopnea" 1×6 double "Hypopnea" 1×6 double "Hypopnea" 1×6 double "Normal" 1×6 double "Normal" 1×6 double "Normal" 1×6 double "Hypopnea" 1×6 double "Hypopnea"
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
Xfeat2 = 10×1 table
Var1 __________ "Hypopnea" "Hypopnea" "Hypopnea" "Hypopnea" "Hypopnea" "Normal" "Normal" "Normal" "Hypopnea" "Hypopnea"

Stephen23
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'})
T1 = 10×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Label ________ ________ _________ _______ ________ ________ ________ 0.089289 0.12105 0.44055 0.78619 0.5783 0.49238 Hypopnea 0.14026 0.98213 0.18146 0.59072 0.079402 0.64926 Normal 0.91114 0.70968 0.83825 0.78321 0.25092 0.43633 Normal 0.58606 0.30577 0.17281 0.30828 0.94065 0.021542 Hypopnea 0.12884 0.30738 0.0020186 0.01869 0.38514 0.45546 Hypopnea 0.8363 0.5931 0.49401 0.68917 0.19066 0.35942 Normal 0.53065 0.038068 0.9396 0.68979 0.54371 0.79645 Normal 0.41558 0.81803 0.13095 0.80943 0.62396 0.48268 Normal 0.32772 0.2482 0.53737 0.82553 0.68154 0.95853 Normal 0.68063 0.53372 0.28868 0.82927 0.13091 0.45976 Normal
T2.Label = categorical({'Normal';'Hypopnea';'Hypopnea';'Hypopnea';'Hypopnea';'Normal';'Normal';'Normal';'Hypopnea';'Hypopnea'})
T2 = 10×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Label _______ ________ ________ ________ ________ _________ ________ 0.83201 0.62591 0.072087 0.226 0.87616 0.38997 Normal 0.84074 0.11763 0.35808 0.82154 0.14292 0.0058556 Hypopnea 0.36574 0.30521 0.82171 0.7092 0.75534 0.53333 Hypopnea 0.55502 0.90404 0.31014 0.68507 0.45954 0.6906 Hypopnea 0.35472 0.081465 0.40127 0.2254 0.059621 0.80679 Hypopnea 0.23728 0.64749 0.68033 0.78142 0.6713 0.10198 Normal 0.92698 0.14575 0.77331 0.59567 0.12626 0.67265 Normal 0.80706 0.26028 0.39637 0.081793 0.32774 0.36823 Normal 0.69254 0.97409 0.81645 0.12883 0.56663 0.47115 Hypopnea 0.61742 0.64129 0.77355 0.33023 0.72177 0.94938 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)
D = 10×1 cell array
{'Hypopnea'} {'Hypopnea'} {'Hypopnea'} {'Hypopnea'} {'Hypopnea'} {'Normal' } {'Normal' } {'Normal' } {'Hypopnea'} {'Hypopnea'}

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!