Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How to resolve Matlab error: Cell contents reference from a non-cell array object.; while solving tsp using particle swarm optimization

Asked by shruti Gandhi on 15 May 2013
function scores = traveling_salesman_fitness(x,distances)
%TRAVELING_SALESMAN_FITNESS  Custom fitness function for TSP.
%   SCORES = TRAVELING_SALESMAN_FITNESS(X,DISTANCES) Calculate the fitness
%   of an individual. The fitness is the total distance traveled for an
%   ordered set of cities in X. DISTANCE(A,B) is the distance from the city
%   A to the city B.
   scores = zeros(size(x,1),1);
   for j = 1:size(x,1)
    % here is where the special knowledge that the population is a cell
    % array is used. Normally, this would be pop(j,:);
    p = abs(x{j}); % HERE APPEARS THE ERROR
    f = distances((p(end)),(p(1)));
     for i = 2:length(p)
        f = f + distances(p(i-1),p(i));
     end
    scores(j) = f;
  end

0 Comments

shruti Gandhi

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 15 May 2013

There is nothing in what you show that would establish for us that x will be a cell array when it is passed in. We need to see how it is constructed.

0 Comments

Walter Roberson

Contact us