[英]Undefined function or variable “number” in matlab
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
Hi all, I try to write a function in Matlab to refer to a value in a multi-dimensional Matrix. 大家好,我尝试在Matlab中编写一个函数来引用多维矩阵中的值。 When I have this function, and I try to pass the index=[2,1,2], sizes=[3,2,2] BP(a 3-dimensional matrix I have already defined) into my CPTValues function, I get the error: 当我有此功能时,我尝试将index = [2,1,2],size = [3,2,2] BP(我已经定义的3维矩阵)传递到CPTValues函数中,错误:
"Undefined function or variable "number" "
Is there anybody here could help me with that, Thanks a lot~ 这里有人可以帮我吗,非常感谢〜
here is a example of the CPTs %P_\\theta(HD|CH,BP,G) 这是CPT%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
The for goes from i=m:1
, but matlab does not understand that i
must decrease instead of increase! for来自i=m:1
,但是matlab不了解i
必须减少而不是增加! change the for line to for i=m:-1:1
, it will do it. 将for行更改为for i=m:-1:1
,它将执行此操作。
EDIT2: 编辑2:
It works ok for me: 对我来说还可以:
%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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.