2014-04-12 74 views
0

我正在使用两个单元存储matlab中神经网络过程的目标和期望值。我已经使用两个1 * 1单元格阵列分别存储值。这是我的代码。如何比较matlab中的两个单元格元素?

cinfo=cell(1,2) 
cinfo(1,1)=iter(1,10)%value is retrieved from a dataset iter 
cinfo(1,2)=iter(1,11) 
amp1=cinfo(1,1); 
amp2=cinfo(1,2); 
if amp1 == amp2 
     message=sprintf('NOT DETECTED BY THE DISEASE'); 
     uiwait(msgbox(message)); 

但是当我运行上面的代码中,出现以下错误:

??? Undefined function or method 'eq' for input arguments of type 'cell'. 
Error in ==> comparison at line 38 
if amp1 == amp2 

如何解决这个问题呢?

+1

我会建议探索[cellfun](http://www.mathworks.in/help/matlab/ref/cellfun.html)并从中找出它。 – Divakar

+0

@Divakar:谢谢。你已经看过cellfun了。但cellfun可用于比较两个数字。 ? – user3368213

+0

你有单元格的数组数组,并回答你的问题是 - 是的。 – Divakar

回答

0

问题是你如何索引的东西。一个1x1的单元阵列不使一个很大的意义,而不是用花括号获得单个细胞的实际元素,通过索引:

amp1=cinfo{1,1}; # get the actual element from the cell array, and not just a 
amp2=cinfo{1,2}; # 1x1 cell array by indexing with {} 
if (amp1 == amp2) 
    ## etc... 

但请注意,如果amp1amp2没有标量以上将采取行动奇怪的。相反,做

if (all (amp1 == amp2)) 
    ## etc... 
0

使用isequal。即使电池的内容有不同的尺寸,这也可以工作。

实施例:

cinfo=cell(1,2); 
cinfo(1,1) = {1:10}; %// store vector of 10 numbers in cell 1 
cinfo(1,2) = {1:20}; %// store vector of 20 numbers in cell 2 
amp1 = cinfo(1,1); %// single cell containing a length-10 numeric vector 
amp2 = cinfo(1,2); %// single cell containing a length-20 numeric vector 
if isequal(amp1,amp2) 
    %// ... 

在这个例子中,它平行代码,amp1amp2是由含有数值向量的单个细胞的细胞阵列。另一种可能性是直接存储每个单元的内容amp1amp2,然后对它们进行比较:

amp1 = cinfo{1,1}; %// length-10 numeric vector 
amp2 = cinfo{1,2}; %// length-20 numeric vector 
if isequal(amp1,amp2) 
    %// ... 

注意,即使在这种情况下,比较amp1==amp1all(amp1==amp2)会给一个错误,因为向量具有不同大小。