The bottleneck in my program is computing the sign of a number for all numbers in an array, when the array size is very large. I show the two approaches I've tried below, both with similar results. I have 16GB of RAM, and the array occupies ~5GB. The problem I'm seeing is the sign function takes up a lot of RAM+virtual memory. Anyone know of a way to reduce the memory requirements and speed up this process for putting the sign of array input into array output (see below)?
Using a for loop with if or switch commands doesn't run out of memory, but takes an hour to complete (way 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.