2014-04-12 73 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)會給一個錯誤,因爲向量具有不同大小。