How to speed up iterating a large data in a nested loop ?

1 次查看(过去 30 天)
%example
a=1x1000;
b=1x1000;
for i=1:length(a)
for j 1:length(b)
x(i,j)=cos(a(i));
y(i,j)=sin(b(j));
end
end
Thank you and appreciate!

回答(1 个)

Image Analyst
Image Analyst 2020-12-5
Try this:
% Create sample data.
% numPoints = 500;
a = pi * rand(1, numPoints);
b = pi * rand(1, numPoints);
% Create x and y
x = repmat(cos(a(:)), [1, numPoints]);
y = repmat(sin(b), [numPoints, 1]);
% Display the x and y matrices:
cmap = jet(256);
subplot(2, 1, 1);
imshow(x, [], 'Colormap', cmap);
colorbar;
title('x', 'FontSize', 20);
subplot(2, 1, 2);
imshow(y, [], 'Colormap', cmap);
colorbar;
title('y', 'FontSize', 20);
  2 个评论
HN
HN 2020-12-6
编辑:HN 2020-12-6
Image Analyst , Actually the code in the original quesion has been just example not a real issue. In the actual problem, the input cannot be moidified as you put it in the answer. Below is the actual problem and the computation time is quite long. Can it be still improved ?
Thank you .
th_int =1X1000 % value cannot be changed
psi_int = 1X1000 % value cannot be changed
phi_int = 1X1000 % value cannot be changed
for k=1: length(th_int)
for j=1:length(th_int)
th(j) = th_int(j);
psi(k) = psi_int(k);
phi(j) =phi_int(j);
R=Rot('z',th(j))*Rot('y',psi(k))*Rot('x',phi(j)); % rotation matrix
p=[Pose(1,k); Pose(2,k);Pose(3,k)];
a1(:,k)=R*[rp;0;0];
a2(:,k)=R*[rp*cos(alpha);rp*sin(alpha);0];
a3(:,k)=R*[rp*cos(beta);rp*sin(beta);0];
r1=p+R*[rp;0;0];
r2=p+R*[rp*cos(alpha);rp*sin(alpha);0];
r3=p+R*[rp*cos(beta);rp*sin(beta);0];
g1=inv(Rot('z',0))*r1;
g2=inv(Rot('z',alpha))*r2;
g3=inv(Rot('z',beta))*r3;
b1(k)=g1(1)+sqrt(L^2-g1(3)^2);
b2(k)=g2(1)+sqrt(L^2-g2(3)^2);
b3(k)=g3(1)+sqrt(L^2-g3(3)^2);
% passive koint value
sin_th21=(g1(1)-b1(k))/L;
cos_th21=g1(3)/L;
th21(k)=atan2(sin_th21,cos_th21);
sin_th22=(g2(1)-b2(k))/L;
cos_th22=g2(3)/L;
th22(k)=atan2(sin_th22,cos_th22);
sin_th23=(g3(1)-b3(k))/L;
cos_th23=g3(3)/L;
th23(k)=atan2(sin_th23,cos_th23);
%%
pr1(k)=norm(r1-p); % radius 1
pr2(k)=norm(r3-p); % radius 2
pr3(k)=norm(r3-p); % radius 3
r12(k)=norm(r1-r2);
r23(k)=norm(r3-r2);
r31(k)=norm(r3-r1);
p11=Rot('z',0)*[b1(k)*1000;0;0];
p12=Rot('z',2*pi/3)*[b2(k)*1000;0;0];
p13=Rot('z',4*pi/3)*[b3(k)*1000;0;0];
L1(k)=norm(r1-[b1(k);0;0]);
L2(k)=norm(r2-Rot('z',2*pi/3)*[b2(k);0;0]);
L3(k)=norm(r3-Rot('z',4*pi/3)*[b3(k);0;0]);
o=[0;0;0];
figure(1)
clf;
hold on
drawVector([o,p11],'b2*');
drawVector([o,p12],'b2*');
drawVector([o,p13],'b2*');
drawLine([p11,r1*1000],'b2*');
drawLine([p12,r2*1000],'b2*');
drawLine([p13,r3*1000],'b2*');
drawLine([o,[1707;0;0]],'k');
drawLine([o,Rot('z',2*pi/3)*[1707;0;0]],'k--');
drawLine([o,Rot('z',-2*pi/3)*[1707;0;0]],'k--'); %beta(j)
drawLine([r1*1000 r2*1000]);
drawLine([r2*1000 r3*1000]);
drawLine([r3*1000 r1*1000]);
xlim ([-2000 2000]);
ylim ([-2000 2000]);
zlim ([-250 1000]);
axis square;
hold on;
pause(0.01)
Rx(k,j)=(th(j));
Ry(k,j)=(psi(k));
Rz(k,j)=(phi(k));
XX(k,j)=p(1);
YY(k,j)=p(2);
ZZ(k,j)=p(3);
end
end
Image Analyst
Image Analyst 2020-12-6
If things depend on only k, and not j, then put them between the k and j loop, not inside the j loop.

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by