This is the solution:
for i=1:size(nodes,2)
nodestotal(:,i)=nodes(:,i);
index1=(size(nodes,2)+1:size(nodes,2)-Vtype:totalnodes);
index2=(size(nodes,2)+2:size(nodes,2)-Vtype:totalnodes);
for ii=size(nodes,2)+1:size(nodes,2)-Vtype:totalnodes
nodestotal(1,ii)=nodes(1,1);
nodestotal(2,ii)=nodes(2,1);
idx1=find(index1==ii);
nodestotal(3,ii)=((2*idx1)+1)*H-(idx1+1)*d;
for jj=size(nodes,2)+2:size(nodes,2)-Vtype:totalnodes
nodestotal(1,jj)=nodes(1,2);
nodestotal(2,jj)=nodes(2,2);
idx2=find(index2==jj);
nodestotal(3,jj)=((2*idx2)+1)*H-idx2*d;
end
end
end