Improve efficiency of array multiplication
조회 수: 4 (최근 30일)
이전 댓글 표시
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);
SW(ind)=sum(matObj_read.W(:,ind));
WS = W.*SPS(ind);
SWS(ind) = sum(WS);
end
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.
댓글 수: 0
답변 (2개)
David Young
2015년 4월 6일
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);
Mahdiyar
2015년 4월 6일
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
Regrads
댓글 수: 9
Mahdiyar
2015년 4월 8일
I do not know what kind of code you want to implement. But if I were you, I try to delete the variables which are not needed anymore.
Moreover, if your code can be divided in different parts,
1- run this part of code separately. It means that you can run your code untill this part and save the matrix matObj_read.W like bellow
save('matObj_read.W ', 'matObj_read.W ')
2- then clear all the variables and run this part of code that you mention at this question and save the final matrix.
3- Again delete all the variable and just load the final matrix and run the rest of code.
Running the First Part
.
.
.
.
clearvars -except matObj_read.W
Runing this part of code
.
.
.
.
clearvars -except Final_Matrix
Running the rest of code
Regards,
참고 항목
카테고리
Help Center 및 File Exchange에서 Performance and Memory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!