Error using vertcat Dimensions of arrays being concatenated are not consistent.

5 views (last 30 days)
I want to make the results for each iteration to display not just the final answer. where the first column is the iteration number and the second column is the result of each iteration.
RRTSS is a called file with an output of " ans = [ t1 dist Reach ] "
size is another variable that contain output.
but the error came out. can anyone help me ??
Ni=2
T = [];
for iteration=1:Ni
RRTSS1
T = [T; iteration size ans] ;
end
T
output :
Error using vertcat Dimensions of arrays being concatenated are not consistent.
  7 Comments
ARIFF HAIKAL
ARIFF HAIKAL on 20 Nov 2019
clc; close;
openfig('newfullterrain25.fig');hold on
Xs=2; Ys=11; Zs=1; Xg=22; Yg=17; Zg=1; Qfree=64;
n=1000; step=0.7; r=sqrt((6*Qfree*log(n))/(pi*n))
s=min(r,step); P=zeros(1,n);
CON=zeros(n); Near=zeros(n); Cost=625*ones(1,n); Cost(1)=0;
size=1; Reach=0; N=zeros(2,n); N(1,1)=Xs; N(2,1)=Ys; N(3,1)=Zs;
tic;
while size<n
if sqrt((N(1,size)-Xg)^2 +(N(2,size)-Yg)^2 +(N(3,size)-Zg)^2 )<step
Reach=1; ReachG=size;
end
%%%%%%%%%%%%%%% Xrand (Collision Free) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CD=0;
while CD==0
Xc=24.5*rand(1)+0.25;
Yc=24.5*rand(1)+0.25;
Zc=24.5*rand(1)+0.25;
Xn=Xc; Yn=Yc; Zn=Zc; simple1ColDec
end
Xrand=Xc; Yrand=Yc; Zrand=Zc;
%%%%%%%%%%%%%%% Xnearest %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:size
if sqrt((N(1,i)-Xrand)^2+(N(2,i)-Yrand)^2 +(N(3,i)-Zrand)^2 )<Inf
Inf=sqrt((N(1,i)-Xrand)^2+(N(2,i)-Yrand)^2 +(N(3,i)-Zrand)^2 );
Xnearest= N(1,i); Ynearest=N(2,i); Znearest=N(3,i); nearest=i;
end
end
%%%%%%%%%%%%%%% Xnew %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xc=Xnearest; Yc=Ynearest; Zc=Znearest; CD=1;
D=sqrt((Xnearest-Xrand)^2+(Ynearest-Yrand)^2 +(Znearest-Zrand)^2 );
if step<=sqrt((Xnearest-Xrand)^2 +(Ynearest-Yrand)^2 +(Znearest-Zrand)^2)
Xn=((D-step)/D)*Xnearest+(step/D)*Xrand;
Yn=((D-step)/D)*Ynearest+(step/D)*Yrand;
Zn=((D-step)/D)*Znearest+(step/D)*Zrand;
else
Xn=Xrand; Yn=Yrand; Zn=Zrand;
end
simple1steer
if CD==1
Xnew=Xrand; Ynew=Yrand; Znew=Zrand;
else
Xnew=((v-1)/625)*Xrand+(1-(v-1)/625)*Xnearest;
Ynew=((v-1)/625)*Yrand+(1-(v-1)/625)*Ynearest;
Znew=((v-1)/625)*Zrand+(1-(v-1)/625)*Znearest;
end
%%%%%%%%%%%%%%% Xnear (Finding near nodes) %%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:size
if sqrt((N(1,i)-Xnew)^2 +(N(2,i)-Ynew)^2 +(N(3,i)-Znew)^2 )<=s
Xc=N(1,i); Yc=N(2,i); Zc=N(3,i); Xn=Xnew; Yn=Ynew; Zn=Znew; simple1ColDec
if CD==1
Near(i,size+1)=1; Near(size+1,i)=1;
end
end
end
%%%%%%%%%%%%%%% Parent (Expanding the tree) %%%%%%%%%%%%%%%%%%%%%%%%%
size=size+1;
N(1,size)=Xnew; N(2,size)=Ynew; N(3,size)=Znew; Inf=101; h=1;
for i=1:size-1
if Near(i,size)==1 && Cost(i)+sqrt((N(1,i)-Xnew)^2 +(N(2,i)-Ynew)^2 +(N(3,i)-Znew)^2 )<Inf
Inf=Cost(i)+sqrt((N(1,i)-Xnew)^2 +(N(2,i)-Ynew)^2 +(N(3,i)-Znew)^2 );
parent=i; h=0;
end
end
if h==1
parent=nearest;
end
P(size)=parent;
CON(parent,size)=sqrt((N(1,parent)-Xnew)^2 +(N(2,parent)-Ynew)^2 +(N(3,parent)-Znew)^2 );
CON(size,parent)=CON(parent,size);
Cost(size)=Cost(parent)+CON(parent,size);
%%%%%%%%%%%%%%% ReWire %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for i=1:size-1
if Near(i,size)==1 && i~=P(size) && ...
Cost(size)+sqrt((N(1,i)-Xnew)^2 +(N(2,i)-Ynew)^2 +(N(3,i)-Znew)^2 )<Cost(i)
CON(i,P(i))=0; CON(P(i),i)=0;
P(i)=size;
CON(i,size)=sqrt((N(1,i)-Xnew)^2 +(N(2,i)-Ynew)^2 +(N(3,i)-Znew)^2 );
CON(size,i)=CON(i,size);
Cost(i)=Cost(size)+CON(i,size);
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
size
% H = size ;
% [H]
end
t1=toc
Reach;
if Reach==1
N(1,size+1)=Xg; N(2,size+1)=Yg; N(3,size+1)=Zg;
CON(ReachG,size+1)=sqrt((N(1,ReachG)-Xg)^2 +(N(2,ReachG)-Yg)^2 +(N(3,ReachG)-Zg)^2 );
CON(size+1,ReachG)=CON(ReachG,size+1);
end
T=linspace(0,2*pi);
for i=1:size
plot3(N(1,i),N(2,i), N(3,i),'go','markersize',2,'MarkerFaceColor','g');
%text(N(1,i), N(2,i),num2str(i));
%plot(N(1,i)+s*cos(T),N(2,i)+s*sin(T),'r');
for j=1:size
if CON(i,j)>0 && j>i
plot3([N(1,j) N(1,i)], [N(2,j) N(2,i)], [N(3,j) N(3,i)],'b','linewidth',1);
end
end
end
if Reach==1
G=sparse(CON);
[dist, path]=graphshortestpath(G, 1, size+1); l=length(path);
for i=1:l-1
plot3([N(1,path(i)) N(1,path(i+1))], [N(2,path(i)) ...
N(2,path(i+1))], [N(3,path(i)) N(3,path(i+1))],'k','linewidth',3);
end
end
% if Reach==1
% [t1 dist Reach]
% else
% Reach=0;
% end
ans = [t1,dist,Reach]
xlabel ('x')
ylabel ('y')
zlabel ('z')
plot3(Xs,Ys,Zs,'--rs','LineWidth',2.5,'MarkerEdgeColor','k',...
'MarkerFaceColor','y','MarkerSize',10); hold on
plot3(Xg,Yg,Zg,'--rs','LineWidth',2.5,'MarkerEdgeColor','k',...
'MarkerFaceColor','g','MarkerSize',10)
Jan
Jan on 20 Nov 2019
Edited: Jan on 20 Nov 2019
Do not overload important Matlab commands, because this must end in confusions. "size", "Inf", "ans", "path".
Which line is cause the error?
We cannot run your code due to the missing script "simple1ColDec".
Using l as name of a variable provokes the confusion with 1. Using one command per line is recommended also.

Sign in to comment.

Answers (1)

Ridwan Alam
Ridwan Alam on 20 Nov 2019
Edited: Ridwan Alam on 20 Nov 2019
In the RRTSS1 script, "dist" is defined inside an "if" statement without any "else" for "dist" value.
So if Reach=0, then ans = [t1 [] Reach] which is not 3x1, rather a 2x1 vector.
This is causing the dimension mismatch.
if Reach==1
G=sparse(CON);
[dist, path]=graphshortestpath(G, 1, size+1); l=length(path);
for i=1:l-1
plot3([N(1,path(i)) N(1,path(i+1))], [N(2,path(i)) ...
N(2,path(i+1))], [N(3,path(i)) N(3,path(i+1))],'k','linewidth',3);
end
else
dist = 0;
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!