Compare two columns having different values.
1 次查看(过去 30 天)
显示 更早的评论
I have 2 variables A and B, I want to compare the values with each other and want result like variable C. Is there any direct function I can use to get C?
A={'a','b','c','d','e'}
B={'b','c','d','e','f'}
C={'a',-
'b','b'
'c','c'
'd','d'
'e','e'
-.'f'}
0 个评论
回答(2 个)
Arif Hoq
2022-3-28
try this:
A={'a','b','c','d','e'};
B={'b','c','d','e','f'};
C={'a','-'
'b','b'
'c','c'
'd','d'
'e','e'
'-.','f'};
a=[A(2:end) B(1:end-1)]';
b=[[A(1) '-'];reshape(a,[],2)];
out=[b;['-.' B(end)]]
4 个评论
Arif Hoq
2022-3-29
A={'a','b','c','d','e'};
B={'b','c','d','e','f'};
for i=1:length(A)
for j=1:length(B)
if any(strcmp(A(i),B(j)))==1
C(i,:)=[A(i) B(j)];
end
end
end
output=[A(1) '-';C(2:end,:);'-.' B(end)]
MJFcoNaN
2022-4-1
I will suggest the table join to do it:
tbla=table;
tblb=table;
tbla.var={'a','b','c','d','e'}';
tblb.var={'b','c','d','e','f'}';
tblc=outerjoin(tbla, tblb);
C=tblc{:,:};
C(cellfun(@isempty,C))={'-'};
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!