Table comparison [String - cell compare]

20 ビュー (過去 30 日間)
Khaled Genidy
Khaled Genidy 2021 年 11 月 11 日
編集済み: Seth Furman 2021 年 11 月 15 日
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 件のコメント
the cyclist
the cyclist 2021 年 11 月 11 日
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.

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

回答 (2 件)

Seth Furman
Seth Furman 2021 年 11 月 12 日
編集済み: Seth Furman 2021 年 11 月 15 日
Take a look at the table functions innerjoin and outerjoin:
t1 = table((1:5)', categorical(["XW";"XR";"XR";"XW";"XX"]), 'VariableNames', ["X","Y"])
t1 = 5×2 table
X Y _ __ 1 XW 2 XR 3 XR 4 XW 5 XX
t2 = table((5:-1:1)', categorical(["XW";"XR";"XR";"XZ";"XZ"]), 'VariableNames', ["X","Y"])
t2 = 5×2 table
X Y _ __ 5 XW 4 XR 3 XR 2 XZ 1 XZ
tJoined = innerjoin(t1, t2, "Keys", "Y")
tJoined = 6×3 table
X_t1 Y X_t2 ____ __ ____ 2 XR 4 2 XR 3 3 XR 4 3 XR 3 1 XW 5 4 XW 5
tJoined = mergevars(tJoined, ["X_t1", "X_t2"], "NewVariableName", "X")
tJoined = 6×2 table
X Y ______ __ 2 4 XR 2 3 XR 3 4 XR 3 3 XR 1 5 XW 4 5 XW
[groupNums, Y] = findgroups(tJoined.Y)
groupNums = 6×1
1 1 1 1 2 2
Y = 2×1 categorical array
XR XW
X = splitapply(@(x) {unique(x)'}, tJoined.X, groupNums)
X = 2×1 cell array
{[2 3 4]} {[1 4 5]}
table(X, Y)
ans = 2×2 table
X Y _________ __ {[2 3 4]} XR {[1 4 5]} XW

Image Analyst
Image Analyst 2021 年 11 月 12 日
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'}

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by