How to speed up iterating a large data in a nested loop ?
조회 수: 2 (최근 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!