Matlab: is there a way to speed up the calculation of the sign of a number?

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.

+3
source share
2 answers

for, , , , :

chunkSize = 1e7;
for start=1:chunkSize:size
    stop = min(start+chunkSize, size);
    output(start:stop) = int8(sign(input(start:stop)-scalar));
end

, , / . ( ), :

input = rand(size, 1, 'single');
output = zeros(size, 1, 'int8') - 1;
+6

, sign .

, , output 1 0 ,

siz = 1e9; %# do not use 'size' as a variable, since it an important function
input = rand(siz,1,'single'); %# this directly creates a single array
scalar = single(0.5);
output = input>scalar;

, . , ? - , , .

+1

Source: https://habr.com/ru/post/1771805/


All Articles