How to deal with a huge matrix?

Hi!
I'm dealing with a huge matrix: to give a little context if it makes sense, it is a transition matrix of a MDP, I thus have to calculate first all the transition probabilities (==> many loops) and then use this matrix in Bellman's equation (==> many matrices products)
In clear, I must create a big matrix (~1GB, potentially more) in my program before using it several times.
My question is, should I rather:
1 - divide my program in several parts (one little "mother function" calling the calculation function, getting the big matrix back, and launching the second function that uses it), which is generally advised?
2 - or put all in the same function in an ugly way :)
Because I'm afraid Matlab will make many copies of the matrix with the first solution. If the total size used by Matlab gets bigger than my RAM, it could cause troubles.
Thanks!
Martin

 Risposta accettata

Matt J
Matt J il 18 Lug 2013
Modificato: Matt J il 18 Lug 2013

1 voto

Because I'm afraid Matlab will make many copies of the matrix with the first solution.
MATLAB never copies a matrix as it gets passed around to different functions unless a change is made to the matrix by one of the functions.
As for the size of the matrix itself, have you considered constructing it using the SPARSE command? Or is the matrix inherently dense?

4 Commenti

Martin
Martin il 18 Lug 2013
Ok, thank you for your answer. I want to be sure that I understood well your first sentence, I take an example:
% mother function, that I will call from the command window
m = create_matrix(); % Big matrix created
m(1,4) = 46; % does not make a copy of m?
f(m);
g(m);
% end of mother function
% function f
function f(m)
disp(m); No copy?
end
% function g function g(m); m(2,4) = 56; % Matlab makes at this moment a copy of the entire matrix m?
end
Hopes it's clear enough. :)
Thanks
Martin
Martin il 18 Lug 2013
And I'm already using sparse matrices, thanks for the advice, though.
Matt J
Matt J il 18 Lug 2013
Modificato: Matt J il 19 Lug 2013
% function g function g(m); m(2,4) = 56;
This is the only point in what you've shown where a copy of m would be made. Here's some further relevant reading:
Basically, for the most part, MATLAB is smart enough never to make copies unless it needs them. Applying RESHAPE to a matrix also doesn't create copies.
Extracting a sub-matrix by indexing, however, does allocate memory for the sub-matrix, unless the indexing is done on the left hand side of an assignment. As an example, B(1:4) is allocated memory below
A=rand(5);
B=1:8;
C=A(B(1:4),:); %The submatrix of B here occupies its own block of memory.
Conversely, the following
B(:)=1;
does not result in any new memory allocation for the already existing B. The values of B simply get over-written in place. In
B(1:4)=2;
memory might be allocated for the index vector 1:4 itself (depending on your MATLAB version) but not for the altered B.
Martin
Martin il 19 Lug 2013
Thank you for your answer.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by