MATLAB Answers

The dimensions of the array are different, but I don't know which part is wrong.

2 views (last 30 days)
주원 박
주원 박 on 13 Sep 2021
Answered: Prateek Rai on 15 Sep 2021
Hi, i have 1 problem as follow:
================================================================================================
>> Example1_T_Matrix1
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in Example1_T_Matrix1 (line 30)
T01 = [R_z_1 P_0;0 0 0 1];
================================================================================================
P_0 = [0 0 0]; % 0벡터
P_1 = [0 0.1 0.2]; % 1번 프레임에서 Linkage 길이
P_2 = [0 -0.1 0.6]; % 2번 프레임에서 Linkage 길이
P_3 = [0 0.2 0.6]; % 3번 프레임에서 Linkage 길이
P_4 = [0 0.1 0.1]; % 4번 프레임에서 Linkage 길이
theta_1 = 0/180*pi; % 1축 회전각
theta_2 = 0/180*pi; % 2축 회전각
theta_3 = 0/180*pi; % 3축 회전각
theta_4 = 0/180*pi; % 4축 회전각
% R_x = [1 0 0; 0 cos(theta) -sin(theta); 0 sin(theta) cos(theta)];
% R_y = [cos(theta) 0 sin(theta); 0 1 0; -sin(theta) cos(theta)];
% R_z = [cos(theta -sin(theta) 0; sin(theta) cos(theta) 0; 0 0 1];
R_z_1 = [cos(theta_1) -sin(theta_1) 0;
sin(theta_1) cos(theta_1) 0;
0 0 1]; % Frame 1 z축 회전
R_x_2 = [1 0 0;
0 cos(theta_2) -sin(theta_2);
0 sin(theta_2) cos(theta_2)]; % Frame 2 x축 회전
R_x_3 = [1 0 0;
0 cos(theta_3) -sin(theta_3);
0 sin(theta_3) cos(theta_3)]; % Frame 3 x축 회전
R_y_4 = [cos(theta_4) 0 sin(theta_4);
0 1 0;
-sin(theta_4) 0 cos(theta_4)]; % Frame 4 y축 회전
% T행렬 작성
T01 = [R_z_1 P_0;0 0 0 1];
T12 = [R_x_2 P_1;0 0 0 1];
T23 = [R_x_3 P_2;0 0 0 1];
T34 = [R_y_4 P_3;0 0 0 1];
% T행렬 합성
T02 = T01*T12; % 0 Frame에서 본 2 Frame
T03 = T01*T12*T23; % 0 Frame에서 본 3 Frame
T04 = T01*T12*T23*T34; % 0 Frame에서 본 4 Frame
% Global 좌표계(0 Frame)에서 본 각 Frame 별 링키지
P_1_G = T01*[P_1;1];
P_2_G = T02*[P_2;1];
P_3_G = T03*[P_3;1];
P_4_G = T04*[P_4;1];
% 링키지 표시(선분의 경우 시점과 종점 좌표가 필요)
L1 = [P_0 P_1_G(1:3,1)];
L2 = [P_1_G(1:3,1) P_2_G(1:3,1)];
L3 = [P_2_G(1:3,1) P_3_G(1:3,1)];
L4 = [P_3_G(1:3,1) P_4_G(1:3,1)];
% 좌표계 표시(x, y, z좌표의 시점과 종점, 크기는 0.1)
x_L = [0 0.1;0 0;0 0;1 1];
y_L = [0 0;0 0.1;0 0;1 1];
z_L = [0 0;0 0;0 0.1;1 1];
% Global 좌표계에서 본 각 좌표계
x_1 = T01*x_L;
y_1 = T01*y_L;
z_1 = T01*z_L;
x_2 = T02*x_L;
y_2 = T02*y_L;
z_2 = T02*z_L;
x_3 = T03*x_L;
y_3 = T03*y_L;
z_3 = T03*z_L;
x_4 = T04*x_L;
y_4 = T04*y_L;
z_4 = T04*z_L;
close all
figure(1)
% Frame plot
plot3(x_1(1,:), x_1(2,:),x_1(3,:),'r', x_2(1,:),x_2(2,:),x_2(3,:),'r',...
x_3(1,:),x_3(2,:),x_3(3,:),'r', x_4(1,:),x_4(2,:),x_4(3,:),'r', 'LineWidth',1)
% 여러개를 하나의 figure 위에 그리기 위해
hold on
plot3(y_1(1,:), y_1(2,:),y_1(3,:),'g', y_2(1,:),y_2(2,:),y_2(3,:),'g',...
y_3(1,:),y_3(2,:),y_3(3,:),'g', y_4(1,:),y_4(2,:),y_4(3,:),'g', 'LineWidth',1)
plot3(z_1(1,:), z_1(2,:),z_1(3,:),'b', z_2(1,:),z_2(2,:),z_2(3,:),'b',...
z_3(1,:),z_3(2,:),z_3(3,:),'b', z_4(1,:),z_4(2,:),z_4(3,:),'b', 'LineWidth',1)
plot3(L1(1,:),L1(2,:),L1(3,:),'-o','color',[0, 0.4470, 0.7410],'LineWidth',2)
plot3(L2(1,:),L2(2,:),L2(3,:),'-o','color',[0.8500, 0.3250, 0.0980],'LineWidth',2)
plot3(L3(1,:),L3(2,:),L3(3,:),'-o','color',[0.9290, 0.6940, 0.1250],'LineWidth',2)
plot3(L4(1,:),L4(2,:),L4(3,:),'-o','color',[0.4940, 0.1840, 0.5560],'LineWidth',2)
grid on
axis equal
tilte('robot')
figure(2)

Answers (1)

Prateek Rai
Prateek Rai on 15 Sep 2021
To my understanding, you are getting an array because dimensions of arrays being concatenated are not consistent.
The error is proabably arrising on line 30 i.e.,
T01 = [R_z_1 P_0;0 0 0 1];
Here, size of R_z_1 is [3, 3] , Size of P_0 is [1, 3].
To concatenate two matrices, they must have compatible sizes. In other words, when you concatenate matrices horizontally, they must have the same number of rows.
Thus,
[R_z_1 P_0]
Here, number of rows is not same thus they are not compatible and hence it is throwing error. Now since,
[R_z_1 P_0]
is not compatible, thus,
[R_z_1 P_0;0 0 0 1]
is also not compatible.
You can refer to following answer to find more on a possible workaround. You can also refer to Creating, Concatenating, and Expanding Matrices MathWorks documentation page to find more information on concatenation in general.

Community Treasure Hunt

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

Start Hunting!