【问题标题】:Undefined function or variable "number" in matlabmatlab中未定义的函数或变量“数字”
【发布时间】:2013-02-08 11:58:09
【问题描述】:
 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 函数中,我得到错误:

"Undefined function or variable "number" "

这里有没有人可以帮助我, 非常感谢~

这是 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

【问题讨论】:

  • 能否举个CTPS矩阵的例子,让我们测试一下函数?
  • 请注意,如果长度 >2,您的第二个 for 循环也不会执行任何操作

标签: matlab multidimensional-array


【解决方案1】:

for 来自 i=m:1 ,但 matlab 不明白 i 必须减少而不是增加!将 for 行更改为 for 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.875 但它返回 0 给我。我的方法一定有一些问题。你能再帮我一次吗?我只想获取多数组的值。提前致谢
  • 对于第二个 for 循环,我认为它确实"count=count*round(array(1,i));"当长度大于两个时。
  • @lexie 你想用这段代码实现什么?只是获取多维数组中的值?你为什么不直接'CTPS(索引)'?
  • 我知道我可以使用索引,但是教授要求编写一个方法来编写一个方法,该方法将访问任何 CPT 的任何条目,其中任何数量的父母在时间上与父母的数量成线性关系.这就是我想用这段代码实现的目标
  • @lexie,好的,我会试着检查一下。拜托,你能发布你的代码的真实部分吗?我无法运行您发布的最后一篇文章,因为它至少缺少 Data 和 m。
猜你喜欢
  • 2012-04-15
  • 2013-02-24
  • 2023-03-08
  • 2013-09-27
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-17
相关资源
最近更新 更多