2013-02-08 149 views
0
function parameter=CPTValues(index,sizes,CPTS) 
    m=size(index,2); 
    n=size(sizes,2); 
    for i=m:1 
     number=(round(index(1,i)-1))*arraySizes(sizes,n); 
     n=n-1; 
    end 
    parameter=CPTS(round(number)); 
end 

function arraySize=arraySizes(array,length) 
    count=1; 
    if (length>=2) 
     for i=length-1:1 
      count=count*round(array(1,i)); 
     end 
     arraySize=round(count); 
    else 
     arraySize=1; 
    end 
end 

大家好,我嘗試在Matlab中編寫一個函數來引用多維矩陣中的值。當我有這個函數,並且我試圖通過index = [2,1,2],sizes = [3,2,2] BP(我已經定義了一個3維矩陣)到我的CPTValues函數中,我得到錯誤:未定義的函數或matlab中的變量「數字」

"Undefined function or variable "number" "

是否有任何人在這裏可以幫助我與, 非常感謝〜

這裏是彩管 %P_ \ theta的一個例子(HD | CH,BP, G)

HD=zeros(2,2,2,2); 
    for i=1:m 
     for ch=1:2 
      for bp=1:2 
       for g=1:2 
       for hd=1:2 
        if(Data(i,5)==ch&&Data(i,4)==bp&&Data(i,2)==g&&Data(i,9)==hd) 
         HD(ch,bp,g,hd)=HD(ch,bp,g,hd)+1; 
        end 
       end 
      end 
     end 
    end 
end 
PCBG=zeros(2,2,2); 
for i=1:m 
    for ch=1:2 
     for bp=1:2 
      for g=1:2 
       if(Data(i,5)==ch&&Data(i,4)==bp&&Data(i,2)==g) 
        PCBG(ch,bp,g)=PCBG(ch,bp,g)+1; 
       end 
      end 
     end 
    end 
end 

for ch=1:2 
    for bp=1:2 
     for g=1:2 
      HD(ch,bp,g,:)=HD(ch,bp,g,:)/PCBG(ch,bp,g); 
     end 
    end 
end 
+1

你可以給CTPS矩陣的例子,所以我們可以測試的功能? – 2013-02-08 12:04:36

+1

請注意,如果長度> 2,您的第二個for循環也不會執行任何操作。 – 2013-02-08 12:20:05

回答

1

該代碼從i=m:1,但matlab不明白,i必須減少而不是增加!將該行更改爲i=m:-1:1,它將執行此操作。

EDIT2:

它好的工作對我來說:

%Create random BP of sizes=[3,2,2] 
BP=rand(sizes) 
BP(:,:,1) = 
    0.9572 0.1419 
    0.4854 0.4218 
    0.8003 0.9157 
BP(:,:,2) = 
    0.7922 0.0357 
    0.9595 0.8491 
    0.6557 0.9340 
%Set an index to look 
index=[2,1,2]; 
%try the function 
CTPValues(index,sizes,BP) 
    ans = 
     0.9572 
%try indexing the matrix directly 
BP(2,1,2) 
    ans = 
    0.9595 
+0

非常感謝。現在這種方法可以運行,但不能給我我想要的答案。它應該給我0.875的答案,但它返回給我。關於我的方法肯定有一些問題。你能再次幫助我嗎?我只想獲得多數組的值。預先感謝 – lexie 2013-02-08 12:34:52

+0

第二for循環,我認爲它「count = count * round(array(1,i));」當長度大於二時。 – lexie 2013-02-08 12:42:35

+0

@lexie你在用這段代碼試圖實現什麼?只需獲取多維數組內的值?你爲什麼不直接'CTPS(索引)'? – 2013-02-08 12:48:41