how can i distribute a large matrix?
조회 수: 4 (최근 30일)
이전 댓글 표시
I have a matrix of (20*2^20) and I can not deal with it . I want to distribute the matrix. How can I use ( distribute) and (gather) . please, the steps that can I write in the m-file.
댓글 수: 11
답변 (2개)
Image Analyst
2015년 1월 4일
If a lot of it is empty/zero/unassigned, then try making it sparse. Otherwise, use it in bite-sized chunks.
댓글 수: 0
Stephen23
2015년 1월 10일
The code that you uploaded is full of poor coding practice. Some examples of this are:
- using i and j as the names of variables (there are the names of the inbuilt imaginary unit ).
- over-complicated data structure: use a cell array instead of a structure.
- no vectorization : total reliance on loops.
- no array preallocation within those loops.
Using distributed arrays will not solve the slowness of this calculation, because it is the code itself that it poorly written. The last two points particularly need to be addresses to speed up your calculation.
If you tried to use the MATLAB debugger , then you would quickly find that the script does not even progress past the first set of nested loops. When I investigated why this is so slow, I found that the variable a is being enlarged on every loop iteration:
a = [0,1]
a = [0,1,0,1]
a = [0,1,0,1,0,1]
..
a = [0,1,0,1,0,1,0,1,.. ???]
given the rather obfuscated nature of how this vector is being generated I did not waste my time trying to figure out how large it might become (when I stopped it was 1x115444). Every time that you extend any variable in MATLAB it has to check the available memory, and move the variable if required. Suffice to say this can be a slow process.
Note also that you could generate this vector all in one go using the much simpler command
a(2:2:N) = 1;
for some scalar N. Try it and you will see how much faster MATLAB can be.
Until this code is vectorized where appropriate and plenty of array preallocation where not, then this code will always be slow, regardless of what computer you run it on.
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!