The bottleneck in my program calculates the sign of the number for all numbers in the array when the size of the array is very large. I show two approaches that I tried below, both with similar results. I have 16 GB of RAM, and the array takes ~ 5 GB. The problem I see is the sign function, which takes up a lot of RAM + virtual memory. Does anyone know a way to reduce memory requirements and speed up this process in order to put an input sign of an array in the output of an array (see below)?
Using a for loop with if or switch commands does not run out of memory, but it takes an hour to complete (too long).
size = 1e9; % size of large array (just an example, could be larger)
output = int8(zeros(size,1)-1); % preallocate to -1
input = single(rand(size,1)); % create random array between 0 and 1
scalar = single(0.5); % just a scalar number, set to 0.5 (midpoint) for example
% approach 1 (comment out when using approach 2)
output = int8(sign(input - scalar)); % this line of code uses a ton of RAM and virtual memory
% approach 2
output(input>scalar) = 1; % this line of code uses a ton of RAM and virtual memory
output(input==scalar) = 0; % this line of code uses a ton of RAM and virtual memory
Thanks in advance for any suggestions.
source
share