A Slow Running Code
Show older comments
As a part of my project, I am using VPI's powermod function for computing matrix raised to a high power. I do know that powermod is faster than MATLAB's power function but my code runs very slowly (takes hours for a matrix of 512x512) and I cannot think of any way to fix it. How to use for-loop efficiently or how to use vectorization to make the code run faster. Any suggestion will be much appreciated. Here is the part of the code:
clc
clear
p = 321321197
q = 321321211
p = vpi(p);
q = vpi(q);
N = p*q;
N = vpi(N);
N2 = vpi(N)^2;
g = N+1;
g = vpi(g);
I = magic(512);
[R, C] = size(I);
test = vpi(zeros(R, C));
for idx = 1 : R
for idy = 1 : C
test(idx, idy) = powermod(vpi(I(idx, idy)), N, N2);
end
end
3 Comments
KSSV
on 22 Mar 2018
First, you need to run a profiler and check where maximum time is spent. Read about profiling.
Walter Roberson
on 22 Mar 2018
What happens if you skip the loops and try
test = powermod(vpi(I), N, N2)
Mohsin Shah
on 22 Mar 2018
Answers (0)
Categories
Find more on Other Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!