【问题标题】:Matlab filtering two strings to make them equalMatlab过滤两个字符串以使它们相等
【发布时间】: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',这是你想要的吗?

标签: string matlab


【解决方案1】:

通过循环解决这个问题,你走在了正确的轨道上,但我觉得你的想法有点太复杂了。看看这个解决方案

A=['a';'b';'b';'b';'c';'d'];
B=['a';'b';'b';'d'];

% allocate logical vectors
lgA = false(size(A));
lgB = false(size(B));

% initialize loop control counter
ia0 = 1;
% looping
for ib = 1:length(B)
    for ia = ia0:length(A)
        % check if A == B and stop the looping over A if it is true
        if A(ia) == B(ib)
            % store 
            lgA(ia) = true;
            lgB(ib) = true;
            % update loop control counter to start from the current
            % index the next time
            ia0 = ia+1;
            break
        end
    end
end
% validity check
assert( all(A(lgA) == B(lgB)) )
% output
APos = find(lgA);
BPos = find(lgB);

【讨论】:

  • 这会返回一个正确的解决方案,但不是最长的匹配。试试A=['a';'b';'c';'d';'e';'z']; B=['a';'z';'b';'c';'d';'e'];。我确信这个问题没有最优的贪心算法。
  • 这是正确的@Daniel。它假定您要找到第一个匹配项。如果您想找到最长的,则必须将其打包在B 上的附加循环中,从而改变for ib-loop 的初始位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 2015-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多