【发布时间】:2020-03-22 09:52:36
【问题描述】:
我需要比较两个字符数组并使它们完全相同。同时,我需要我保存的角色的坐标。
比如我有两个字符数组
A=['a';'b';'b';'b';'c';'d'];
B=['a';'b';'b';'d'];
我希望结果是
Anew=['a';'b';'b';'d'];
Bnew=['a';'b';'b';'d'];
我想知道职位
APos=[1;2;3;6];
BPos=[1;2;3;4];
这样
Anew=A(APos);
Bnew=B(BPos);
我尝试了以下代码
[lmod1,lmod2]=size(A);
[lobs1,lobs2]=size(B);
PosB=zeros(lobs1,1);
for i=1:lobs1
for j=1:lmod1
if B(i,:)==A(j,:)
PosB(i,:)=i; %collect the ones that are in there, the zeros should then be the ones that are not
end
end
end
PosB2=PosB(PosB~=0);
PosA=zeros(lmod1,1);
for i=1:lmod1
for j=1:lobs1
if A(i,:)==B(j,:)% if this is true at the end of the loop, it means that the value of namesobs is in namesw
PosA(i,:)=i; %collect the ones that are in there, the zeros should then be the ones that are not
end
end
end
PosA2=PosA(PosA~=0);
但这只会检查是否存在,而不是如果这些值重复了不同的次数。如何添加一个额外的过滤器,以便字符串在两个字符串数组中重复相同的次数?
仅供参考,我正在使用的实际字符串如下所示:
>> ODates(1:10,:)
ans =
10×12 char array
'20100202_186'
'20100202_186'
'20100202_190'
'20100202_190'
'20100202_190'
'20100202_190'
'20100202_190'
'20100202_191'
'20100202_191'
'20100202_191'
EDates(1:10,:)
ans =
10×12 char array
'20100202_186'
'20100202_190'
'20100202_190'
'20100202_190'
'20100202_190'
然后答案是:
NewDates(1:10,:)
ans =
10×12 char array
'20100202_186'
'20100202_190'
'20100202_190'
'20100202_190'
'20100202_190'
【问题讨论】:
-
快速提问:你说过,字符串应该是一样的,但是 'all(A(APos) == B(BPos))' 是假的,因为里面有 3 个 ''b''
A你说,你想要的结果是APos=[1;2;3;5;6];...现在你能澄清你想要什么吗? -
@max 打错了谢谢 ´APos = [1;2;3;6];´
-
对可以出现的字符串有什么限制吗?总是按字母顺序排列的东西?如果一个角色出现多次,它们是相邻的吗?一般情况可以解决,但这样的信息会更容易。
-
@Daniel 我添加了一个我的实际 char 数组的示例。
-
看到这样的例子我有点惊讶。假设你有'20100202_123'和'20100123_456',结果是'20100123',这是你想要的吗?