Remove the repetitive items in matrix

2 次查看(过去 30 天)
The following codes are some basic settings, you can skip this section and directly see the problem section below.
clear
clc
% forward kinematics
syms the1 the2 the3 the4 the5
a1 = 100;
a2 = 120;
a3 = 94;
Px = cos(the1)*(a3*cos(the2 + the3) + a2*cos(the2));
Py = sin(the1)*(a3*cos(the2 + the3) + a2*cos(the2));
Pz = a1 + a3*sin(the2 + the3) + a2*sin(the2);
t1=0/180*pi;
t2=35/180*pi;
t3=-60/180*pi;
px_new=vpa(subs(Px,[the1,the2,the3],[t1,t2,t3]));
py_new=vpa(subs(Py,[the1,the2,the3],[t1,t2,t3]));
pz_new=vpa(subs(Pz,[the1,the2,the3],[t1,t2,t3]));
% inverse kinematics
[t1,t2,t3] = solve(Px==px_new,Py==py_new,Pz==pz_new,the1,the2,the3)
% convert the unit from radian to degree
Theta1=vpa((t1)*180/pi);
Theta2=vpa((t2)*180/pi);
Theta3=vpa((t3)*180/pi);
% From Forward H03, get R03
R03 = [cos(the2 + the3)*cos(the1) -sin(the2 + the3)*cos(the1) sin(the1);
cos(the2 + the3)*sin(the1) -sin(the2 + the3)*sin(the1) -cos(the1);
sin(the2 + the3) cos(the2 + the3) 0]
% From Forward H46, get R46
R46 = [cos(the4)*cos(the5) -cos(the4)*sin(the5) sin(the4);
cos(the5)*sin(the4) -sin(the4)*sin(the5) -cos(the4);
sin(the5) cos(the5) 0]
% Use values replace symbols
R03_1 = vpa(subs(R03,[the1,the2,the3],[t1(1),t2(1),t3(1)]))
R03_2 = vpa(subs(R03,[the1,the2,the3],[t1(2),t2(2),t3(2)]))
R03_3 = vpa(subs(R03,[the1,the2,the3],[t1(3),t2(3),t3(3)]))
R03_4 = vpa(subs(R03,[the1,the2,the3],[t1(4),t2(4),t3(4)]))
R06 = [1 0 0;
0 1 0;
0 0 1];
% Get values in number form
R46_1 = (R03_1)'*R06
R46_2 = (R03_2)'*R06
R46_3 = (R03_3)'*R06
R46_4 = (R03_4)'*R06
% R46_all = [R46_1 R46_2 R46_3 R46_4]
The problems appear in the following codes. The output of t41,t51,t42,t52 has repetitive items, I want to remove the repetitive items. Then, I can combine them using t4 = [t41,t42,t43,t44] and t5 = [t51,t52,t53,t54]. Finally, I can use Theta4=vpa((t4)*180/pi) and Theta5=vpa((t5)*180/pi) to represent neatly the same as my Theta1,Theta2 and Theta3. Thank you in advance!
% How to remove the repetitive items and combine [t41,t42,t43,t44] into t4
% and t5 matrices?
[t41,t51] = solve(sin(the4)==R46_1(1,3),sin(the5)==R46_1(3,1),the4,the5)
[t42,t52] = solve(sin(the4)==R46_2(1,3),sin(the5)==R46_2(3,1),the4,the5)
[t43,t53] = solve(sin(the4)==R46_3(1,3),sin(the5)==R46_3(3,1),the4,the5)
[t44,t54] = solve(sin(the4)==R46_4(1,3),sin(the5)==R46_4(3,1),the4,the5)
Theta4_1 = vpa((t41)*180/pi)
Theta5_1 = vpa((t51)*180/pi)
Theta4_2 = vpa((t42)*180/pi)
Theta5_2 = vpa((t52)*180/pi)
Theta4_3 = vpa((t43)*180/pi)
Theta5_3 = vpa((t53)*180/pi)
Theta4_4 = vpa((t44)*180/pi)
Theta5_4 = vpa((t54)*180/pi)
t4 = [t41,t42,t43,t44];
t5 = [t51,t52,t53,t54];
Theta4=vpa((t4)*180/pi);
Theta5=vpa((t5)*180/pi);
  3 个评论
dpb
dpb 2021-11-26
编辑:dpb 2021-11-26
It's always good to have enough info; but sometimes there's too much!
Can't see the forest for the trees, here, but look at
doc unique
to solve the problem of eliminating duplicates.
If the values are floating point and not integral-valued, then
doc ismembertol
may be helpful.
You could probably simplify the whole thing a bunch by using either cell arrays or arrays of a one-higher dimension to replace all (or at least many of) the sequentially-named variables in which you could then process by looping or maybe even vectorize.
It is a sure sign almost always that one is not using MATLAB syntax efficiently when these variable naming schemes show up.
Matthew Worker
Matthew Worker 2021-11-26
Thank you for your suggestions! Next time I will try to simplify my codes.

请先登录,再进行评论。

采纳的回答

DGM
DGM 2021-11-26
编辑:DGM 2021-11-26
You're using solve to solve two independent equations as if they were a system, and solve is accordingly returning a set of points. I don't see why any of that is necessary. These are independent simple periodic functions. Just evaluate the arcsin.
Instead of doing all this:
% How to remove the repetitive items and combine [t41,t42,t43,t44] into t4
% and t5 matrices?
[t41,t51] = solve(sin(the4)==R46_1(1,3),sin(the5)==R46_1(3,1),the4,the5)
[t42,t52] = solve(sin(the4)==R46_2(1,3),sin(the5)==R46_2(3,1),the4,the5)
[t43,t53] = solve(sin(the4)==R46_3(1,3),sin(the5)==R46_3(3,1),the4,the5)
[t44,t54] = solve(sin(the4)==R46_4(1,3),sin(the5)==R46_4(3,1),the4,the5)
Theta4_1 = vpa((t41)*180/pi)
Theta5_1 = vpa((t51)*180/pi)
Theta4_2 = vpa((t42)*180/pi)
Theta5_2 = vpa((t52)*180/pi)
Theta4_3 = vpa((t43)*180/pi)
Theta5_3 = vpa((t53)*180/pi)
Theta4_4 = vpa((t44)*180/pi)
Theta5_4 = vpa((t54)*180/pi)
t4 = [t41,t42,t43,t44];
t5 = [t51,t52,t53,t54];
Theta4=vpa((t4)*180/pi);
Theta5=vpa((t5)*180/pi);
Why not just do it directly?
% symbolic form, column vector
Theta4 = asind([R46_1(1,3) R46_2(1,3) R46_3(1,3) R46_4(1,3)].')
Theta5 = asind([R46_1(3,1) R46_2(3,1) R46_3(3,1) R46_4(3,1)].')
Theta4 =
-78.539816339744830961566084581988/pi
135.16677730298831568345876865956/pi
135.16677730298831568345876865956/pi
-78.539816339744830961566084581988/pi
Theta5 =
-5.7781949423580712925184862590422e-38/pi
-5.7781949423580712925184862590422e-38/pi
0
0
If you want a numeric result, cast it using double().

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by