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

n=20;
xleft = -100;
xright = 100;
yfront = 0;
yback = 200;
step = 1;
[x,y] = meshgrid(xleft:step:xright,yfront:step:yback);
faces = delaunay(x,y);
a=0;
for i=xleft:step:yback
a = a+1;
b = 0;
for j=yfront:step:yback
b = b+1;
if i>-10 && i<50 &&i>30 &&j<50
z(a,b) = 15;
else
z(a,b) = 0;
end
end
end
vertices = [x(:) y(:) z(:)];
orig = [0 0 20];
%dir = [-10 20 -2];
vert1 = vertices(faces(:,1),:);
vert2 = vertices(faces(:,2),:);
vert3 = vertices(faces(:,3),:);

4 Comments

Check the sizes of x, y, z..they are not same so error.
NOTE: Don't attch your code as image snippet. You should copy paste here.
n=20;
xleft = -100;
xright = 100;
yfront = 0;
yback = 200;
step = 1;
[x,y] = meshgrid(xleft:step:xright,yfront:step:yback);
faces = delaunay(x,y);
a=0;
for i=xleft:step:yback
a = a+1;
b = 0;
for j=yfront:step:yback
b = b+1;
if i>-10 && i<50 &&i>30 &&j<50
z(a,b) = 30;
else
z(a,b) = 0;
end
end
end
vertices = [x(:) y(:) z(:)];
orig = [0 0 20];
%dir = [-10 20 -2];
vert1 = vertices(faces(:,1),:);
vert2 = vertices(faces(:,2),:);
vert3 = vertices(faces(:,3),:);
am not sure what's size of x,y,z.
just follow teacher's code, is same but not work foe me.
The dimensions of x, y, z are 201*201, 201*201, 301*201 respectively.....so you cannot join them..
yep found that, but dont know how to fix that

Sign in to comment.

Answers (1)

for i=xleft:step:yback
That is dubious, especially considering
for j=yfront:step:yback
what happened to xright ?
[x,y] = meshgrid(xleft:step:xright,yfront:step:yback);
for j=yfront:step:yback
Technically speaking, it is not guaranteed that the colon operator in a for loop will give exactly the same values as the colon operator that you used in your meshgrid call. You should do something like
xvec = xleft:step:xright;
yvec = yfront:step:yback;
[x, y] = meshgrid(xvec, yvec);
and later
for i=xvec
for j = yvec
Your double for loop can be optimized away as
a = zeros(size(x));
a( x>-10 & x<50 & y>30 & y<50) = 30;
with no loop at all.

Categories

Products

Release

R2019b

Asked:

on 6 Oct 2020

Answered:

on 6 Oct 2020

Community Treasure Hunt

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

Start Hunting!