How to replace elements if it is true
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I have below cell array matrices:
A={'DF','3';'FR','4';'RR','2';'RT','4'};
B={'AB','1';'BC','3';'CD','5';'DF','7';'FR','9';'RT','8';'TA','1';'AR','3';'TY','4'};
if any of the row in B is member in A, the replace the corresponding second column element by seconf column element of A. I use the below code,
close all
clear all
clc
A={'DF','3';'FR','4';'RR','2';'RT','4'};
B={'AB','1';'BC','3';'CD','5';'DF','7';'FR','9';'RT','8';'TA','1';'AR','3';'TY','4'};
indx=ismember(B(:,1),A(:,1));
B(indx,2)=A(indx,2);
but it give the error:
??? Index exceeds matrix dimensions.
Error in ==> ScoreCompare at 7 B(indx,2)=A(indx,2);
My desired output should be:
AB 1
BC 3
CD 5
DF 3
FR 4
RT 4
TA 1
AR 3
TY 4
Many thanks in advance.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!