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!
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Blue 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!