2017-03-22 90 views
0
% 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 

我實際上使用這個代碼來將矩陣的所有非空元素都替換爲1,並且讓元素爲空的零點。我的問題是:有沒有更好的方法來使用matlab函數來做到這一點?在Matlab中用矩陣替換矩陣的所有非空元素的最佳方法是什麼?

回答

2

我只想用MATLAB的邏輯索引功能,即

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

當然

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

s(s ~= 0) = 1; 

也會工作。

+0

謝謝@Andreas H. –

+0

或's = double(邏輯(s))' –

+0

@Luis Mendo。誠然,本着同樣的精神,也可以寫成's = double(u〜= 0)' –

相關問題