Improve efficiency of array multiplication

5 views (last 30 days)
I hope someone can help me with improving the efficiency of my code. I have an array of 65536x65536 elements stored in a matfile. Now I would like to multiplicate each column with 65536 different constants. I have solved this as followed:
for ind=1:65536
W = matObj_read.W(:,ind);
WS = W.*SPS(ind);
SWS(ind) = sum(WS);
In the end the sum (SWS) is what I need to use
This code works, however, it takes like forever and since I need to repeat this calculation to often the total calculations time is arround a few years.... I have tried to make te code faster by removing the for loop and read the full variable W, but then I recieved an error for array size limit. This was simply solved but returned in an out of memmory error. I hope some can help me with making the code more efficient.

Answers (2)

David Young
David Young on 6 Apr 2015
The value of SW is not used in the code you've shown. If you don't need the value of SW, it will help to omit this line. If you do need SW later, then you can probably speed up the computation by replacing the line that computes it with
SW(ind) = sum(W);
which avoids repeating the operations that get W.
It will also help to preallocate the arrays. That is, before the loop, do
sz = 65536;
SW = zeros(1, sz);
SWS = zeros(1, sz);
  1 Comment
Ramona Bouwman
Ramona Bouwman on 6 Apr 2015
sorry, I needed to remove the SW(ind)= sum(matObj_read.W(:,ind)); line. I indeed pre-allocated the arrays as you suggested.

Sign in to comment.

Mahdiyar on 6 Apr 2015
Hi Bouwman
There are different options that you can consider:
1- For heavy computation, you can try to employ all the core of the Computer/Laptop to be used for Matlab. Have a look at commands "parpool" and "parfor".
2- you can use the cammand repmat to multiply the different constant at the same time. Of course, in the code that you wrote variable "SPS" is not defined. Please look at the following Code.
Matrix = randi(10, 65536, 65536)
Constant = randi(10, 1, 65536)
Repeated_Constant = repmat(Constant, 65536, 1)
Final_Matrix = Matrix .* Repeated_Constant
In this code, I just put random number for Matrix and Constant number that you can substitute with your own matrix.
I run the code for number 4 instead of 65536, I get the following results that clearly explain.
Matrix =
9 7 10 10
10 1 10 5
2 3 2 9
10 6 10 2
Constant =
5 10 8 10
Repeated_Constant =
5 10 8 10
5 10 8 10
5 10 8 10
5 10 8 10
Final_Matrix =
45 70 80 100
50 10 80 50
10 30 16 90
50 60 80 20
Ramona Bouwman
Ramona Bouwman on 8 Apr 2015
Thank you, this indeed improved the efficiency. Only 1/3 of the original time is needed for the calculations.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by