Mahdi Khosravi
Mahdi Khosravi

Reputation: 129

Troubleshooting Out of memory error in MATLAB

How do I concatenate two vectors of length 20,000,000 in MATLAB?

x=randn(1,20000000);

y=x;

z=[x y];

w=[x y];

when I enter w = [x y]; it errors out :

Error using horzcat
Out of memory. Type HELP MEMORY for your options.

    >> [uV sV] = memory

    uV = 

        MaxPossibleArrayBytes: 710127616
        MemAvailableAllArrays: 1.6797e+009
                MemUsedMATLAB: 345354240


    sV = 

        VirtualAddressSpace: [1x1 struct]
               SystemMemory: [1x1 struct]
             PhysicalMemory: [1x1 struct]

    >> sV.VirtualAddressSpace

    ans = 

        Available: 1.6797e+009
            Total: 2.1474e+009

    >> sV.SystemMemory

    ans = 

        Available: 4.4288e+009

    >> sV.PhysicalMemory

    ans = 

        Available: 2.5376e+009
            Total: 3.4889e+009

Upvotes: 1

Views: 280

Answers (4)

Daniel
Daniel

Reputation: 36710

A general answer can be found in the documentation. If this does not resolve your problem, add your code to the question. Maybe preallocation or something else can solve the memory issues.

Upvotes: 1

sebastian
sebastian

Reputation: 9696

There's not much you can do, either there's enough memory or not.

A single 20.000.000 elements double vector takes up 320MB. That makes 640MB for the concatenated version. If your concatenation is like c = [a,b] this will sum up to 1.28 GB (cause you'll have a, b and c). Whether 1.28GB is "much", depends on your machine.

If your machine physically has enough memory you should try clearing eventually existing other large matrices in matlab or even close other applications.

Last but not least: If your vectors contain a lot of zeros, you should check sparse matrices. They only store non-zero elements and hence might take up much less memory.

Upvotes: 1

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

Here is how one typically would do it efficiently:

x = ones(20000000,1);
y = x;
z1 = [x y];
z2 = [x; y];

The first is for horizontal concatenation, the second for vertical. This does not generate any errors for me, even if I make the variable 10x bigger.

If you cannot run the first three lines, you seem to have insufficient memory to store 4x20000000 numbers. You can try whether you have enough memory to store 3x20000000 numbers and use this instead:

x = [x y];

If the error does not occur with my exaple, then you are probably doing something wrong, please show code that reproduces the error then.

Upvotes: 1

Lokesh A. R.
Lokesh A. R.

Reputation: 2366

This document explains how to resolve the error. The reason for the error is mostly due to the memory constraint of the machine rather than MATLAB.

Upvotes: 1

Related Questions