Is there another solution for write the algorithm by matlab?

429 Views Asked by At

I have an algorithm as you can see below and i wrote it with different 2 ways but it seems there are problems for both solutions.is there any alternative to write it with different way?

$$X_i(k+1)= X_i(k)+ 0.1*\sum_{j\in N_i} (X_j(k)-X_i(k))$$

while( k < t )
    for i = 1 : N
       for j = 1 : N

Solution 1---> X(:,k+1) = X(:,k) + 0.1* ((sum(bsxfun(@minus,X(:,k).',X(:,k)),2))); Solution 2---> X(i,k+1) = X(i,k) + 0.1*((( X(j,k)-X(i,k))));

      end
    end
   k = k + 1;
end
1

There are 1 best solutions below

2
On BEST ANSWER

In explicit form

$$X_1(k+1)=X_1(k)+0.1\bigg((X_1(k)-X_1(k))+(X_1(k)-X_2(k))+...+(X_1(k)-X_N(k))\bigg)$$ $$X_2(k+1)=X_2(k)+0.1\bigg((X_2(k)-X_1(k))+(X_2(k)-X_2(k))+...+(X_2(k)-X_N(k))\bigg)$$ $$...$$ $$X_N(k+1)=X_N(k)+0.1\bigg((X_N(k)-X_1(k))+(X_N(k)-X_2(k))+...+(X_N(k)-X_N(k))\bigg)$$

The algorithm should be

while( k < t )
   for i = 1 : N
      sum=0; 
      for j = 1 : N
         sum=sum+0.1*(X(i,k)-X(j,k));
      end
      X(i,k+1)=X(i,k)+sum;
   end
k = k + 1;
end