calculation of a sparse matrix

8 views (last 30 days)
I have a very large matrix, with 3,26,041 rows and columns. I tried doing the simple sparse function for calculating it. But it returns an output as "out of memory" . I have a 64 bit system with 4gb ram. Is there anyway that i can calculate it or do i need a better system ? Can anyone please help, I am new to matlab and hence require urgent help..
  2 Comments
the cyclist
the cyclist on 5 Apr 2011
Can you give a hint at the code your wrote?
Do you know roughly now many non-zero element in the array?
Sushrut Naik
Sushrut Naik on 5 Apr 2011
m = floor((s^2*10^3)/b);
n = floor((d^2*10^3)/b);
A = zeros(m*n,m*n);
index_ij = 0; % initilising the value of variable index_ij as zero
for i = 1:1:m
for j = 1:1:n
index_ij = index_ij+1;
index_kl = 0; % initilising the value of variable index_kl as zero
for k = 1:1:m
for l = 1:1:n
index_kl = index_kl+1;
%xij=[(i-1)+.5]*delta_x;
%yij=[(j-1)+.5]*delta_y;
%xkl=[(k-1)+.5]*delta_x;
%ykl=[(l-1)+.5]*delta_y;
%A(index_ij,index_kl) = [Y_s/(2*pi)]*exp[-[[(a(i,j)-b(k,l))^2+(c(i,j)-d(k,l))^2]/(2*sigma^2)]];
A(index_ij,index_kl) = Y_s/(2*pi)*exp(-(((i-k)*delta_x)^2+((j-l)*delta_y)^2)/(2*sigma_s^2));
% value of the constant terms
% z(k,l) = e(k,l) * 2;
end
end
end
end
the code is as above. i cannot approximate the number of non zero elements, since the matrix A that i am calculating is not being shown. I wish to get the sparse of the matrix A in the above code

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 5 Apr 2011
Sorry if I am saying what you already know, but the way you have defined A, it is decidedly non-sparse. You should not use the pre-allocation to zeros.
You should initialize the matrix A using the "sparse" command, which will define the indexing into A, without allocating memory for the elements. Then you can fill it in with your loops. Hopefully, there are few enough non-zero elements in the matrix to fit into memory.
  3 Comments
the cyclist
the cyclist on 5 Apr 2011
Yes, I mean you should declare A as sparse instead of preallocating with zeros.
The matrix A = zeros(1000,1000) takes up 8,000,000 bytes. It will remain that size no matter how many non-zero elements you fill in. (The zeros and non-zeros take up the same memory.)
The matrix A = sparse(1000,1000) takes up 8,024 bytes; then it takes up more memory for each element you add to it. If final matrix is truly sparse (i.e. very few non-zeros), then it uses far less memory when you fill it.
The essence is that if you define it as sparse, then MATLAB doesn't waste memory storing all those zeros, which it ordinarily would if it was a (default) double-precision array.
[As a side comment, it would be good if you accept this answer, if you feel it helped, so that future questioners might find it more easily.]
Sushrut Naik
Sushrut Naik on 6 Apr 2011
hey thanks..sorry for the delay...i will check it out.
thank you once again..

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!