preallocating sparse 4d matrix

조회 수: 8 (최근 30일)
Michael
Michael 2013년 3월 7일
Hi
I've got some code that preallocates some matrix L = zeros(50,50,50,50); and then iterates along the first two dimensions (for i = 1:50, for j = 1:50...) to populate L. At the end of my code I reshape L into a 2500x2500 matrix, and this is it in usable form.
I want to increase the dimensions from 50 to 100, but it's hitting a memory bottleneck when preallocating, and I can't use spalloc to generate a 4d matrix. Rather than re-writing all my loops, can anyone think of another way to get around this? The end result (a sparse 10000x10000 matrix) should be easy to create, but I can't make it sparse until it's 2d, which is after my calculations.
Thanks for any help Mike
  댓글 수: 5
Michael
Michael 2013년 3월 7일
this method is working, although it doesn't seem I can generate a sparse matrix with int8 inputs, and this is now creating memory problems further down the line where I need to calculate L'*L.
Walter Roberson
Walter Roberson 2013년 3월 7일
편집: Walter Roberson 2013년 3월 7일
Ah yes, sparse is restricted to double or uint8 (or logical, I think).
You could encode the negatives into uint8, e.g., 254 for -2 (which would be the two's complement to make it easier to track.) Doing the translation later might prove to be time consuming. It might be faster to record indices into a lookup table, and do something like
usedidx = find(L);
[idx1, idx2] = idx2subs([N*N, N*N], usedidx);
sparse(idx1, idx2, LookupTable(L(usedidx)), ...) %recheck arg order!
Notice I did an implicit reshape there by way of the first argument to idx2subs()

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

채택된 답변

James Tursa
James Tursa 2013년 3월 7일
For multi-dimensional sparse arrays, see this FEX submission by Matt J:
MATLAB only supports double and logical sparse arrays. For int8 sparse arrays, you will have to wait until I release my int8sp class in a month or two (basic code is mostly complete, but not fully tested yet).

추가 답변 (1개)

Teja Muppirala
Teja Muppirala 2013년 3월 7일
You can't make a sparse array with more than 2 dimensions, but you could make a 100x100 cell array that is filled with 100x100 sparse matrices. And then you can work on each cell separately, and then go back and shape it into a 10000x10000 afterwards.
L = repmat({sparse(100,100)} ,100,100);
for i = 1:100
for j = 1:100
L{i}{j} = ...
end
end

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by