用Matlab替换矩阵中所有非空元素的最佳方法是什么?

时间:2017-03-22 15:28:48

标签: matlab

% estimation of the jacobian sparse matrix
u_iterator=0;
s=eye(size(u),size(u));
for u_iterator=1:size(u)
    if u(u_iterator) >1e-5
       s(:,u_iterator)=1;
    end
end

我实际上是使用这段代码用矩阵替换矩阵的所有非空元素,而我让零元素为空。我的问题是:使用matlab函数有没有更好的方法呢?

1 个答案:

答案 0 :(得分:2)

我只想使用MATLAB的逻辑索引功能,即

s = u;
s( s > 1e-5 ) = 1;

当然

s( abs(s) > 1e-5 ) = 1;

s( s ~= 0 ) = 1;

也可以。