Reshaping is causing memory problem

조회 수: 4 (최근 30일)
Saddam N Y
Saddam N Y 2024년 2월 5일
댓글: Saddam N Y 2024년 2월 6일
Hello everyone,
I have 500 matrices of huge size of 40834 x 40834 and I would like to load them from mat files and then flatten them using the reshape function and later do some other things. However in the first flatteing operation I am getting a memory issue and Matlab aborts. How one could solve such a problem ? The code is below, here all Ki.mat are sparse matrices. Thank you.
i=1;
K_matrix_file = strcat('Matrices/K',num2str(i),'.mat');
load(K_matrix_file);
K_flatten = reshape(K_i,1,[]);
Saddam
  댓글 수: 3
Matt J
Matt J 2024년 2월 5일
편집: Matt J 2024년 2월 5일
Are the matrices in sparse format? If they are in non-sparse format and they loaded sucessfully, there's no reason the reshape operation should have breached memory limits, unless the matrix consumes most of your RAM.
Saddam N Y
Saddam N Y 2024년 2월 5일
Sorry I updated the question to include the piece of code that produces the issue. The matrices are all in the sparse format.

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

채택된 답변

Matt J
Matt J 2024년 2월 5일
편집: Matt J 2024년 2월 5일
Don't make long sparse row vectors, as these do consume lots of memory. Work with column vectors instead:
K_flatten = K_i(:);
  댓글 수: 2
Walter Roberson
Walter Roberson 2024년 2월 5일
t0 = spalloc(1,0,2);
t1 = spalloc(1,100,2);
t2 = spalloc(1,101,2);
whos t0 t1 t2
Name Size Bytes Class Attributes t0 1x0 40 double sparse t1 1x100 840 double sparse t2 1x101 848 double sparse
t1(1,100) = 1;
t2(1,100) = 1;
whos t1 t2
Name Size Bytes Class Attributes t1 1x100 840 double sparse t2 1x101 848 double sparse
t1(1,50) = 2;
t2(1,50) = 2;
whos t1 t2
Name Size Bytes Class Attributes t1 1x100 840 double sparse t2 1x101 848 double sparse
In short, a sparse row vector takes 8 bytes plus 8 bytes per column plus 16 bytes per non-zero.
This is a more than the equivalent non-sparse row vector, which would take 8 bytes per column.
Saddam N Y
Saddam N Y 2024년 2월 6일
Thank you very much for your answer !!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by