How can I optimize the execution time of a MATLAB script that uses for loops to process large arrays?
i have a MATLAB script that processes a large matrix (eg 10000×10000) using for loops, but the execution time is very high. My script looks like this:
A = rand(10000, 10000); % Matrice inițială
B = zeros(size(A)); % Matricea rezultată
for i = 1:size(A, 1)
for j = 1:size(A, 2)
B(i, j) = A(i, j) * 2; % Exemplu simplu de procesare
end
end
I know that MATLAB works best with vectorized operations. How could I rewrite this script to remove the loops and reduce the execution time?
I’ve tried using functions like arrayfun, but I’m not sure if it’s the best solution for this problem.
>Solution :
Have you tried using simple MATLAB operations such as B=2*A?