【问题标题】:Why ismember doesn't work properly in my program in Matlab?为什么 ismember 在我的 Matlab 程序中不能正常工作?
【发布时间】:2017-07-23 21:38:18
【问题描述】:

在我程序的这一部分,我想将 ID 变量分配给 RealTagID 变量。然后在第二部分中,我尝试将 ID 变量分配给 RealTagID_NEW。 ID 变量来自我的测量数据,我在下面上传了它(它来自 EPC)。它没有按我的意愿工作,因为 Matlab 9B 和 BB 在第一部分出于某种原因是相同的......

这是我的代码:

 close all
clc

RealTagID=['A3 ' ;'A1 ' ; '9F '  ;'9D ' ; '9B ' ; 'A9 '  ; 'A7 ' ; 'A5 ' ];
%The last two characters of the EPC code of the tags
RealPOSX=[40 31 0 -31 -40 -32 0 +31]; 
%The x positions of fixed tags
RealPosY=[0 27 40 27 0 -27 -40 -27];
% The y positions of fixed tags

for i=1:length(XLocalization)
    temp=Epc{i};
    ID(i,:)=temp(end-2:end);
    %Makes a new variable, called 'ID', which is the last two characters from
    %the measured EPC codes
end

for i =1  :length(RealPOSX)

    idx = all(ismember(ID,RealTagID(i,:)),2)
    pos=find(idx==1);
    POS{i}=pos;
end
%-------------------------------------------------------------------------------------------------------------------------------

RealPOSX_NEW=[20 17 0 -17 -20 -17 0 17]; 
%The x positions of fixed tags
RealPosY_NEW=[0 12 20 12 0 -12 -20 -12];
% The y positions of fixed tags
RealTagID_NEW=['B3 ' ;'B5 ' ; 'B7 '  ;'BB ' ; 'AD ' ; 'AB '  ; 'AF ' ; 'B1 ' ];
%The last two characters of the EPC code of the tags

for i =1  :length(RealPOSX_NEW)
    idxx = all(ismember(ID,RealTagID_NEW(i,:)),2)
    poss=find(idxx==1);
    POSS{i}=poss;


end

在第二部分它甚至是值得的。对于matlab,在这种情况下,B1 等于 BB,B3 也等于 BB,B5 等于 BB,B7 等于 BB,AB 等于 BB。

我想比较它们,如果它们相同,那么让我们为逻辑 1,否则为 0。但它不是那样工作的,因为这个 BB 变量或者我不知道。

我正在使用 ismember 函数,但它似乎不是最好的选择。任何想法?我应该使用什么?

所以我的目标是使 POS 和 POSS 变量相等,这意味着它们都应该是 1x8 单元格,并且每个单元格有 58 个元素。现在其中一些有 116,因为在那种情况下,对于 matlab BB 也等于 9B、B1、B3、B5、B7、B9。所以这样不好,BB应该只与BB相等,而不是其他变量。

这是我的测量结果Measurement

希望你能帮助我,我整天都在做这件事,没有任何进展。

【问题讨论】:

  • 我认为您遇到问题的原因是BB 不是单个数组元素,它是具有2个元素的字符数组。 ismember('BB', '9B') 将演示此错误。除此之外,您的问题有很多多余的信息无法帮助我们回答您的实际问题,并且您的代码不可执行,因为您没有定义多个变量。请提供minimal reproducible example 并提供您想要的结果。
  • 非常抱歉,我更正了!我忘记了第二个循环需要 RealPOSX。我也改变了文字,我希望现在更清楚我的问题是什么。我认为您是对的,但我仍然不知道如何解决问题。
  • 有人知道吗?
  • 我可能会使用strcmp,但您仍然必须使用循环,因为它没有矢量化。

标签: matlab assign


【解决方案1】:

最后我用strfind 解决了这个问题。 这是我的代码,它运行良好。

close all
clc
RealPOSX=[40 31 1 -31 -40 -32 1 +31]; 
%The x positions of fixed tags
RealPosY=[1 27 40 27 1 -27 -40 -27];
% The y positions of fixed tags
RealTagID=['A3 ' ;'A1 ' ; '9F '  ;'9D ' ; '9B ' ; 'A9 '  ; 'A7 ' ; 'A5 ' ];

for i=1:length(XLocalization)
    temp=Epc{i};
    ID(i,:)=temp(end-2:end);

end



for i =1  :length(RealPOSX)        
    idx = strfind(string(ID), RealTagID(i,:))

    pos= find(not(cellfun('isempty', idx)))
    POS{i}=pos;
   end

%-------------------------------------------------------------------------------------------------------------------------------

RealPOSX_NEW=[20 17 0 -17 -20 -17 0 17]; 
%The x positions of fixed tags
RealPosY_NEW=[0 12 20 12 0 -12 -20 -12];
% The y positions of fixed tags
RealTagID_NEW=['B3 ' ;'B5 ' ; 'B7 '  ;'BB ' ; 'AD ' ; 'AB '  ; 'AF ' ; 'B1 ' ];
%The last two characters of the EPC code of the tags

for i =1  :length(RealPOSX)
    %idxx = all(ismember(ID,RealTagID_NEW(i,:)),2)
    %poss=find(idxx==1);
    idxx = strfind(string(ID), RealTagID_NEW(i,:)) 
    poss= find(not(cellfun('isempty', idxx)))
    POSS{i}=poss;

【讨论】:

    猜你喜欢
    • 2020-03-18
    • 2023-03-18
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多