Table comparison [String - cell compare]
39 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have 2 tables that contain the same headers of the first row.
- My goal is to see if the cells contains the same values
- if true - contaminate cells to present the values
From the two tables below.
- I need to have my comparison for example : a cell in X would be 1,5 for Y XW
Part of my code is:
for i=1 : size(Table1)
if strcmpi(Table1.X(i), Table2.X(i) ==1)
X12= cellstr(strcat(....., ',' , ..........)
I am just confused.
I am currently getting - conversion to double from cell is not possible.
Table1 Table 2 expected result
X Y X Y X Y
1 XW 5 XW 1, 5 XW
2 XR 4 XR 2, 4 XR
3 XR 3 XR 3 XR
4 XW 2 XZ
5 XX 1 XZ
1 commentaire
the cyclist
le 11 Nov 2021
It would be much easier to help if you uploaded the two tables in a MAT file. You can use the paperclip icon in the INSERT section of the toolbar.
Réponses (2)
Seth Furman
le 12 Nov 2021
Modifié(e) : Seth Furman
le 15 Nov 2021
Take a look at the table functions innerjoin and outerjoin:
- https://www.mathworks.com/help/matlab/ref/table.join.html
- https://www.mathworks.com/help/matlab/ref/outerjoin.html
t1 = table((1:5)', categorical(["XW";"XR";"XR";"XW";"XX"]), 'VariableNames', ["X","Y"])
t2 = table((5:-1:1)', categorical(["XW";"XR";"XR";"XZ";"XZ"]), 'VariableNames', ["X","Y"])
tJoined = innerjoin(t1, t2, "Keys", "Y")
tJoined = mergevars(tJoined, ["X_t1", "X_t2"], "NewVariableName", "X")
[groupNums, Y] = findgroups(tJoined.Y)
X = splitapply(@(x) {unique(x)'}, tJoined.X, groupNums)
table(X, Y)
0 commentaires
Image Analyst
le 12 Nov 2021
Try this:
% Create saple data:
x1 = [1;2;3;4;5]
y1 = {'XW'; 'XR';'XR';'XW';'XX'}
x2 = [5;4;3;2;1]
y2 = {'XW'; 'XR';'XR';'XZ';'XZ'}
table1 = table(x1, y1, 'VariableNames',{'X', 'Y'})
table2 = table(x2, y2, 'VariableNames',{'X', 'Y'})
% Create output table.
table3 = table({0}, {'?'}, 'VariableNames',{'X', 'Y'});
count = 1;
for row = 1 : size(table1, 1)
thisString = table1.Y{row};
fprintf('Searching for %s.\n', thisString)
if isequal(table1.Y{row}, table2.Y{row})
% Columns y match.
table3.X{count} = unique([table1.X(row), table2.X(row)], 'stable');
table3.Y{count} = thisString;
count = count + 1;
end
end
table3
You'll see
table3 =
3×2 table
X Y
_______ ______
{[1 5]} {'XW'}
{[2 4]} {'XR'}
{[ 3]} {'XR'}
0 commentaires
Voir également
Catégories
En savoir plus sur Characters and Strings 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!