MATLAB: Error : Indices must either be real positive integers or logicals.

We take a look at this line first
X(:,iel) = coordinates(nodes(iel,:),1) ;
put display(iel) infront of this line
display(iel)
X(:,iel) = coordinates(nodes(iel,:),1) ;
iel is indeces, it must be real positive integers or logical. Therefore, 0, negative value, floating value are not allowed.

Best Answer

I think your error is from here
nodes(iel,:)
first you assign zero to nodes with 400 x 8 dimension
nodes = zeros(nel,nnel) ;
later in your reshape you assign dimension 21*21 to NodeNo
NodeNo = reshape(NodeNo,npx,npy);
Then reshape nodes follow below
nodes(:,1) = reshape(NodeNo(1:npx-1,1:npy-1),nel,1);
that's mean now nodes(:,1) has dimesion 441 * 1 then you do similar thing to nodes(:,2), nodes(:,3), nodes(:,4).
Now, Try to think, what is the dimension of nodes(:,5)? originally, you assign zero to nodes(:,5) from 1 to 400, then with new expansion of your nodes, 401 to 441 will be NaN.
At the end
X(:,iel) = coordinates(NaN,1) ;
So how to solve it ? I guess simply to change this line to 21*21 dimension
nodes = zeros(npx,npy) ;
% your npx is 21 dimension, npy is also 21 dimension
Related Question