A compact way to replace zeros with Inf in a matrix

10 ビュー (過去 30 日間)
Sim
Sim 2023 年 10 月 16 日
編集済み: Sim 2023 年 10 月 23 日
Would you be so nice to suggest me a more compact way to replace zeros with Inf in the following matrix? (maybe with just one line of code?)
% Input
A = [0 3 2 5 6;
1 1 4 3 2;
9 0 8 1 1;
5 9 8 2 0;
3 1 7 6 9];
% Replace zeros with Inf
[row,col] = ind2sub(size(A),find(A==0));
for i = 1 : length(row)
A(row(i),col(i))=Inf;
end
% Output
A
A = 5×5
Inf 3 2 5 6 1 1 4 3 2 9 Inf 8 1 1 5 9 8 2 Inf 3 1 7 6 9

採用された回答

J. Alex Lee
J. Alex Lee 2023 年 10 月 16 日
編集済み: J. Alex Lee 2023 年 10 月 16 日
You can implicitly index "linearly" for any arrays - it will do all the ind2sub and sub2ind in the background:
% Input
A = [0 3 2 5 6;
1 1 4 3 2;
9 0 8 1 1;
5 9 8 2 0;
3 1 7 6 9];
B = A;
% Replace zeros with Inf
[row,col] = ind2sub(size(A),find(A==0));
for i = 1 : 3
A(row(i),col(i))=Inf;
end
% Output
A
A = 5×5
Inf 3 2 5 6 1 1 4 3 2 9 Inf 8 1 1 5 9 8 2 Inf 3 1 7 6 9
B(B==0) = Inf
B = 5×5
Inf 3 2 5 6 1 1 4 3 2 9 Inf 8 1 1 5 9 8 2 Inf 3 1 7 6 9
isequal(A,B)
ans = logical
1

その他の回答 (4 件)

Les Beckham
Les Beckham 2023 年 10 月 16 日
編集済み: Les Beckham 2023 年 10 月 16 日
If you want to retain the non-zero elements of A and replace the zeros with Inf, then this is how I would suggest that you do that.
% Input
A = [0 3 2 5 6;
1 1 4 3 2;
9 0 8 1 1;
5 9 8 2 0;
3 1 7 6 9];
A(A==0) = Inf
A = 5×5
Inf 3 2 5 6 1 1 4 3 2 9 Inf 8 1 1 5 9 8 2 Inf 3 1 7 6 9
Note that your loop doesn't do this, it creates a matrix with Inf in the positions of the zeros in A and zero everywhere else. If that is really what you want then you could do that like this.
A = [0 3 2 5 6;
1 1 4 3 2;
9 0 8 1 1;
5 9 8 2 0;
3 1 7 6 9];
B = zeros(size(A));
B(A==0) = Inf
B = 5×5
Inf 0 0 0 0 0 0 0 0 0 0 Inf 0 0 0 0 0 0 0 Inf 0 0 0 0 0
  3 件のコメント
Les Beckham
Les Beckham 2023 年 10 月 16 日
編集済み: Les Beckham 2023 年 10 月 16 日
You are quite welcome.
If you are just getting started with Matlab, I would highly recommend that you take a couple of hours to go through the free online tutorial: Matlab Onramp
Sim
Sim 2023 年 10 月 17 日
thanks :-)

サインインしてコメントする。


Matt J
Matt J 2023 年 10 月 16 日
Allso just for fun.
A = [0 3 2 5 6;
1 1 4 3 2;
9 0 8 1 1;
5 9 8 2 0;
3 1 7 6 9];
A=A+1./(A~=0)-1
A = 5×5
Inf 3 2 5 6 1 1 4 3 2 9 Inf 8 1 1 5 9 8 2 Inf 3 1 7 6 9
  2 件のコメント
Alexander
Alexander 2023 年 10 月 16 日
It can't be shorter. Thumbs up.
Sim
Sim 2023 年 10 月 17 日
Thumb up! :-)

サインインしてコメントする。


Walter Roberson
Walter Roberson 2023 年 10 月 23 日
A = [0 3 2 5 6;
1 1 4 3 2;
9 0 8 1 1;
5 9 8 2 0;
3 1 7 6 9];
A(~A) = inf
A = 5×5
Inf 3 2 5 6 1 1 4 3 2 9 Inf 8 1 1 5 9 8 2 Inf 3 1 7 6 9
  2 件のコメント
J. Alex Lee
J. Alex Lee 2023 年 10 月 23 日
by the way, on huge matrices this is actually faster than testing for zero.
Sim
Sim 2023 年 10 月 23 日
編集済み: Sim 2023 年 10 月 23 日
@Walter Roberson Wow!! Thumb up! :-)

サインインしてコメントする。


Alexander
Alexander 2023 年 10 月 16 日
Only for fun. My maybe a bit old-fashoned approach would be:
B=1./A;
B(B==Inf)=0;
C=1./B
  6 件のコメント
Alexander
Alexander 2023 年 10 月 22 日
Thanks @Stephen23 for the advice and yes, there are precision errors. But I think it depends on the problem you have to solve whether these are significant or not.
Stephen23
Stephen23 2023 年 10 月 23 日
"But I think it depends on the problem you have to solve whether these are significant or not."
I can't think of many problems where a more complex, slower, obfuscated approach with precision errors would be preferred over the simpler, clearer, much more robust approach using indexing. Can you give an example?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by