简体   繁体   English

将matlab稀疏矩阵转换为单精度

[英]Converting matlab sparse matrix to single precision

I want to convert a sparse matrix in matlab to single precision, however it appears that matlab doesn't have single sparse implemented.我想将 matlab 中的稀疏矩阵转换为单精度,但是 matlab 似乎没有实现单稀疏。

Instead of that, I am just planning on checking it the values are outside of the single precision range and rounding them off to the highest and lowest values of the single precision range.相反,我只是计划检查它的值是否超出单精度范围,并将它们四舍五入为单精度范围的最高和最低值。

I'd like to do something like this:我想做这样的事情:

for i = 1:rows
    for j = 1:cols
        if (abs(A(i,j) < 2^-126))
            A(i,j) == 0;
        end
    end
end

However, ths is extremely slow.但是,这非常慢。 Is there another command I can use that will work on sparse matrix class type in MATLAB?我可以使用另一个命令来处理 MATLAB 中的稀疏矩阵类类型吗? I notice most commands don't work for the sparse data type.我注意到大多数命令不适用于稀疏数据类型。

EDIT 1:编辑1:

I also tried the following, but as you can see I run out of memory (the matrix is sparse and is 200K x 200K with ~3 million nonzeros):我还尝试了以下方法,但正如您所见,我的内存不足(矩阵是稀疏的,大小为 200K x 200K,约 300 万个非零):

A(A < 2^-126) = 0
Error using  < 
Out of memory. Type HELP MEMORY for your options.

EDIT 2:编辑2:

Current solution I developed based on input from @rahnema1:我根据@rahnema1 的输入开发的当前解决方案:

% Convert entries that aren't in single precision
idx = find(A); % find locations of nonzeros
idx2 = find(abs(A(idx)) < double(realmin('single')));
A(idx(idx2)) = sign(A(idx(idx2)))*double(realmin('single'));
idx3 = find(abs(Problem.A(idx)) > double(realmax('single')));
A(idx(idx3)) = sign(A(idx(idx3)))*double(realmax('single'));

You can find indices of non zero elements and use that to change the matrix; 您可以找到非零元素的索引并使用它来更改矩阵;

idx = find(A);
Anz = A(idx);
idx = idx(Anz < 2^-126);
A(idx) = 0;

Or more compact: 或者更紧凑:

idx = find(A);
A(idx(A(idx) < 2^-126)) = 0;

However if you want to convert from double to single you can use single function: 但是,如果要从double转换为single,可以使用single函数:

idx = find(A); 
A(idx) = double(single(full(A(idx))));

or 要么

A(find(A)) = double(single(nonzeros(A)));

To convert Inf to realmax you can write: 要将Inf转换为realmax您可以写:

A(find(A)) = double(max(-realmax('single'),min(realmax('single'),single(nonzeros(A)))));

If you only want to convert Inf to realmax you can do: 如果您只想将Inf转换为realmax,您可以:

Anz = nonzeros(A);
AInf = isinf(A);
Anz(AInf) = double(realmax('single')) * sign(Anz(AInf));
A(find(A)) = Anz;

As one still reaches this thread when interested in how to convert sparse to single: an easy answer is to use full() .当人们对如何将稀疏转换为单一感兴趣时仍然会到达这个线程:一个简单的答案是使用full() However, this can be problematic if you rely on the memory savings that sparse gives you, as it basically converts to double first.但是,如果您依赖 sparse 为您提供的内存节省,这可能会出现问题,因为它基本上首先转换为 double。

sparseMatrix = sparse(ones(2, 'double'));
single(full(sparseMatrix)) % works
% ans =
%  2×2 single matrix
%     1     1
%     1     1
single(sparseMatrix) % doesn't work
% Error using single
% Attempt to convert to unimplemented sparse type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM