![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/447218/image.png)
How to speed up iterating a large data in a nested loop ?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
%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 Kommentare
Antworten (1)
Image Analyst
am 5 Dez. 2020
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);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/447218/image.png)
2 Kommentare
Image Analyst
am 6 Dez. 2020
If things depend on only k, and not j, then put them between the k and j loop, not inside the j loop.
Siehe auch
Kategorien
Mehr zu Blue finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!