title: Why MATLAB is showing "Index exceeds matrix dimensions." Error
body: <p>I am new to MATLAB and trying to learn Bayesian Networks. 
  Running this code shows an error </p>
  
  <blockquote>
    <p>Index exceeds matrix dimensions.</p>
  </blockquote>
  
  <p>in line</p>
  
  <pre><code>( for r = 1:length(nodes(root(rr)).values))
  </code></pre>
  
  <p>Please help me out with this.</p>
  
  <p>Thanks in Advance</p>
  
  <pre><code>function [nodes, edges] = bnMsgPassInitiate(nodes, edges, root)
  N = numel(nodes);
  
  for X = 1:N % for every node
  
  nodes(X).lambda = ones(1, length(nodes(X).values)); % l(x) = 1
  
  Z = nodes(X).parents;
  for pa = 1:length(Z) % for each parent of X
      edges(Z(pa),X).lambdaX = ones(1, length(nodes(Z(pa)).values)); % lX(z) = 1
  end  
  
  Y = nodes(X).children;
  for ch = 1:length(Y) % for each child of X
      edges(X,Y(ch)).peyeX = ones(1, length(nodes(X).values)); % peyeY(x)
  end
  
  end
  
  numRoots = length(root);
  for rr = 1:numRoots
     **for r = 1:length(nodes(root(rr)).values)**
      nodes(root(rr)).peye(r) = nodes(root(rr)).CPT(r);
      nodes(root(rr)).P(r) = nodes(root(rr)).CPT(r);
  end
  childrenR = nodes(root(rr)).children;
  for cr = 1:length(childrenR)
      [nodes, edges] = bnMsgPassSendPiMsg(root(rr), childrenR(cr), nodes, edges, []); %A = []
  end
  end
  </code></pre>