how can i distribute a large matrix?

조회 수: 4 (최근 30일)
esraa
esraa 2015년 1월 4일
편집: per isakson 2015년 1월 11일
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
esraa
esraa 2015년 1월 5일
편집: Image Analyst 2015년 1월 5일
matrix:
no of rows = 20
no of column = 2^20
May the problem be in my computer? if so, can I send the m-files to be run on another computer?
Image Analyst
Image Analyst 2015년 1월 5일
Of course you can run your code on another computer. Go ahead.

댓글을 달려면 로그인하십시오.

답변 (2개)

Image Analyst
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.

Stephen23
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.
  댓글 수: 1
esraa
esraa 2015년 1월 10일
편집: per isakson 2015년 1월 11일
thank you very much, how can I construct the matrix b, b: no. of column=20 and no. of rows=2^20=1048576.
the first column=[0 1 0 1 0 1 ........]
the second column=[0 0 1 1 0 0 1 1 0 0 1 1 ........]
the third column=[0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 ..........]
the fourth column=[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1..................]
and so on. instead of the following command
for j=1:20
for k=0:(2^(ns-j))-1
a((2^(j-1))+1+k*(2^j):(2^j)+k*(2^j))=1;
end
b(j,:)=a;
clear a
end
b=b';
to take less time compared with the commands I use

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by