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

29 ビュー (過去 30 日間)
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
  2 件のコメント
Chaya N
Chaya N 2016 年 10 月 20 日
What are the dimensions of your fitness, oldchrom, fitness2 and newchrom variables?
Could you post some sample data?
Khoirunnisya Zawawi
Khoirunnisya Zawawi 2016 年 10 月 21 日
in the first generation, the chromosome size is 10, so the fitness should be 10 too. for fitness2 i think it's combiination from oldchrom and newchrom.
can you show me how to check the size of those matrices? i tried size('matrix name') but it didnt work. thank you

サインインしてコメントする。

採用された回答

KSSV
KSSV 2016 年 10 月 20 日
You are trying to merge matrices of different order in
chrom = [fitness oldchrom;fitness2 newchrom];
You have to check the dimensions of those matrices.
  5 件のコメント
Walter Roberson
Walter Roberson 2016 年 10 月 22 日
At the command line, give the command
dbstop if error
now run the program. When it stops with the error, you can look at size() of the various objects.
Khoirunnisya Zawawi
Khoirunnisya Zawawi 2016 年 10 月 24 日
thank you. i got the size
fitness = 1 1
oldchrom = 20 10
fitness2 = 1 1
newchrom = 20 10

サインインしてコメントする。

その他の回答 (2 件)

Chaya N
Chaya N 2016 年 10 月 23 日
編集済み: Chaya N 2016 年 10 月 23 日
Hello Khoirunnisya, could you please run the following code exactly as it is here and paste the output from your command window?
% -------------------------------------------------------------------------
% 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
fitness
newchrom
oldchrom
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));
chrom = 2;
end
Your command window should display the values of the variables fitness, newchrom, oldchrom and fitness2. Do not worry about the outputs from this function yet, I just want an idea about some of your inputs.
  4 件のコメント
Khoirunnisya Zawawi
Khoirunnisya Zawawi 2016 年 10 月 26 日
編集済み: Khoirunnisya Zawawi 2016 年 10 月 26 日
thank you. i run those code but there's an error
Undefined operator '==' for input arguments of type 'struct'.
Error in totalCost (line 15)
for i = 1 : (find(routes(k,:)==endNode)-1)
this is totalCost.m
% -------------------------------------------------------------
% Objective function / Total cost of the path
% -------------------------------------------------------------
function ObjV = totalCost (routes, distances, nind, endNode, link_matrix)
% function name : totalCost
% function input : 1. The paths / routes from source to destination node
% 2. The distance to each nodes
% 3. The number of chromosomes in the population
% 4. The destination node
% 5. The adjacency matrix of relationship between nodes
% function output : Array of total cost for each routes / paths
ObjV = zeros(nind,1);
for k = 1 : nind
cost = 0;
for i = 1 : (find(routes(k,:)==endNode)-1)
temp = link_matrix(routes(k,i),routes(k,(i+1)));
if temp == 1
cost = cost + distances(routes(k,i),routes(k,(i+1)));
else
cost = 0;
break
end
end
ObjV(k) = cost;
end
end
Chaya N
Chaya N 2016 年 10 月 26 日
編集済み: Chaya N 2016 年 10 月 26 日
That error doesn't make sense. There are no structures being passed as input into totalCost.
The input 'routes' seems to be your array newchrom, which according to your previously provided information, is a 20x10 array.
Please clear out your workspace, re-load all necessary variables and run elitism.m again.

サインインしてコメントする。


Walter Roberson
Walter Roberson 2016 年 10 月 24 日
Change
chrom = [fitness oldchrom;fitness2 newchrom];
to
chrom = [repmat(fitness, size(oldchrom,1), 1), oldchrom; repmat(fitness2, size(newchrom,1), 1), newchrom];
  4 件のコメント
Amrita Rana
Amrita Rana 2018 年 6 月 21 日
Error using vertcat Dimensions of matrices being concatenated are not consistent.
Error in Open_dat (line 89) left = find(diff([0; poss_reg])==1);
Can anybody help me ? i am having this error while implementing ecg signal.
Walter Roberson
Walter Roberson 2018 年 6 月 25 日
That code does not appear anywhere in this Question, so we have to guess about the sizes.
We can guess that your poss_reg is a row vector when the code expects it to be a column vector.
Chances are there are better was of doing whatever test you are implementing.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by