MATLAB Answers

Why is large matrix subtraction making my code so slow?

8 views (last 30 days)
Matt Flood
Matt Flood on 18 Sep 2018
Commented: Matt Flood on 27 Sep 2018
Hello,
I have created a function which handles matrices roughly [10000 x 20000] in size. When I ran my function using the time & run profiler in MatLab, it is telling me that two lines of my code are taking 29s and 22s respectively:
These lines of code are basic matrix operations that shouldn't be computationally expensive to perform. They are equivalent to:
N = rand(10000,20000);
X = [zeros(10000,1) cumsum(N,2)];
M = X(:,5:end) - X(:,1:end-5);
When I run these lines of code in the command window, they only take a second or two at most.
Can anyone please tell me why MatLab is taking so long to calculate these lines inside a function? Thanks a mil!
  3 Comments
Matt Flood
Matt Flood on 27 Sep 2018
Thanks guys, I think it was a virtual memory issue. When I cleared each variable straight after they were used, it quickened the performance time.
It still takes 20s-30s to perform the matrix subtraction, but overall the function is faster.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by