How to solve "Error while using repmat : out of memory"

Hi all,
I was dealing with codes for image processing, where I need to use "repmat" function to create a big 5-d matrix, in a size of [116 116 72 64 64].
And it will give "out of memory" error.
The slice of code is shown below, it will give error when trying to assign new value to w (2nd row):
sq = repmat(sq, [1, 1, 1, 1, nd]);
w = repmat(reshape(w, [1, 1, 1, nd, nd]), [sx, sy, sz, 1, 1]);
raw = cat(4, s0, squeeze(sum(sq .* w, 4)));
Here , before these lines,
sq in [116 116 72 64]
w in [64, 64]
nd = 64, sx = sy = 116, sz = 72
What make things wield is, the size of matrix "sq" is in the same shape. And there will be no error when executing the first line of code, which means we could allocate enough space for a matrix in a size of [116 116 72 64 64].
As you can see, what I wanna do is creating w in the same shape as sp, and perform a element-wise multiplication. But the 2nd line will not work.
I tried to pre-allocate an empty matrix instead, by using zeros, but again get "out of memory" error.
Can anybody help me?
Thanks.

 Accepted Answer

Forget about repmats. Use the original sq and w as follows,
tmp=reshape( reshape(sq,[],nd)*w , size(sq) );
raw = cat(4, s0, tmp);

5 Comments

Hi, thanks for the answer. But it will give error msg:
Error using *
MTIMES (*) is not fully supported for integer classes. At least one argument must be scalar.
Because I forgot to mention, in this case, sq is integer matrix but w is double.
And perhaps the reason sq could survive while w will exceed memory is due to different data type. Integer matrix took up less space.
Convert sq to double or single and then do the multiplication. Since you're not using repmat anymore, you should have plenty enough memory.
Hi, I tried to do that. But For big matrix like sq, originally it stores int type data,so that it could survive. I tried to change format to double, but that will cause out of memory, and can only be changed into single at most. Then, reshaped sq is 62005248*64, and w is 64*64. Since matlab uses double as default, it'll raise memory exceeding error when performing multiplication.
Then, reshaped sq is 62005248*64, and w is 64*64. Since matlab uses double as default, it'll raise memory exceeding error when performing multiplication.
First of all, no, if you multiply a single matrix by a double matrix, the result will be a single matrix.
Secondly, the memory consumption that you've described doesn't add up. You said in your initial post that you had just enough memory to hold the repmatted sq, which was of dimensions [116 116 72 64 64]. That had to consume at least 4 GB. If you had enough memory for that, you should have plenty of memory to spare (now that you're not using repmat) to hold the original sq in single or even double format. In single format, this only consumes 0.25 GB:
>> whos sq
Name Size Kilobytes Class Attributes
sq 116x116x72x64 242208 single
If you're still getting out of memory errors, my suspicion is that you are still using repmat somewhere.
Yes, you're correct.
Double multiplied by a single is still single.
And for sq in 116x116x72x64, even if we set it to double, it's still smaller than repmatted sq.
So it's kind of wield that matlab gave memory exceeding error on these 2 situation, I'll check it out.
But your solution is perfect, thanks, you saved my life.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 4 Jan 2019

Commented:

on 8 Jan 2019

Community Treasure Hunt

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

Start Hunting!