MATLAB: How to solve this error ? Error using horzcat Dimensions of matrices being concatenated are not consistent.

genetic algorithmhorzcat

I found a code about implementation of genetic algorithm in TSP. it's running but suddenly this error appears :
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in elitism (line 15)
chrom = [fitness oldchrom;fitness2 newchrom];
i look for information about horzcat, it means that the array that i wanna combine dont have the same length?
can someone show me how to handle and solve this error. thank you so much!
this is code in elitism.m
% -------------------------------------------------------------------------

% Procedure of Elitism
% -------------------------------------------------------------------------
function chrom = elitism (fitness, newchrom, oldchrom, matrix_cost, nind, endNode, link_matrix)
% function name : elitism
% function input : 1. The fitness of old chromosomes
% 2. The new chromosomes
% 3. The previous / old chromosomes
% 4. The costs
% 5. The number of population
% 6. The destination node
% 7. The adjacency matrix of relationship between nodes
% function output : The chromosomes for the new generation
fitness2 = fitnessV(totalCost(newchrom,matrix_cost,nind,endNode,link_matrix));
chrom = [fitness oldchrom;fitness2 newchrom];
temp = sortrows(chrom);
chrom = temp((size(temp,1)-nind+1):size(temp,1),2:size(temp,2));
end

Best Answer

You are trying to merge matrices of different order in
chrom = [fitness oldchrom;fitness2 newchrom];
You have to check the dimensions of those matrices.