Vinicius Nogueira
Vinicius Nogueira

Reputation: 33

MATLAB - Problems with large arrays

The code below works great for small vectors.

[X1, X2]=meshgrid(Data1(:,1), Data2(:,1));
[Y1, Y2]=meshgrid(Data1(:,2), Data2(:,2));
[Z1, Z2]=meshgrid(Data1(:,3), Data2(:,3));

Rxy = sqrt( (X1-X2).^2 + (Y1-Y2).^2 );
Rz = abs( Z1-Z2 );

[I1, I2] =find( Rxy<=100 & Rz<=0.2);

However, as I work with a large amount of data, matlab does not support and does not work correctly. Matlab generates the following message:

Error using repmat Requested 75027x68517 (38.3GB) array exceeds maximum array size preference. Creation of arrays greater than this limit may take a long time and cause MATLAB to Become unresponsive. See array size limit or preference panel for more information.

Seeking alternatives to "out of memory error," but I'm not getting an efficient manner. I made a loop to function is, but it was extremely slow.

Upvotes: 2

Views: 4717

Answers (1)

EelkeSpaak
EelkeSpaak

Reputation: 2825

Presumably meshgrid is using repmat internally to create some massive matrices. Have a look at bsxfun, allows these types of operations without actually replicating the data. From the help:

C = bsxfun(fun,A,B) applies the element-by-element binary operation specified by the function handle fun to arrays A and B, with singleton expansion enabled.

So line 5 of your example would become (untested, don't have Matlab available right now:

Rxy = sqrt( bsxfun(@minus, Data1(:,1), Data2(:,1)).^2 + bsxfun(@minus, Data1(:,2), Data2(:,2)).^2 );

and change the line below analogously.

Upvotes: 0

Related Questions