Matrix caculation about NAN and IFN

3 ビュー (過去 30 日間)
Jialin Men
Jialin Men 2022 年 6 月 27 日
コメント済み: Jialin Men 2022 年 6 月 27 日
Hello everyone,
I have a question about Matrix cacluation.
I have two Matrix as follows:
C =A./B
so the elements from A divided by B element . than I get NAN and IFN.
How to ignore them?
Many many Thanks
JM

採用された回答

Karim
Karim 2022 年 6 月 27 日
編集済み: Karim 2022 年 6 月 27 日
you can use indexing to avoid deviding by zero:
A = [1 0 0; 1 1 0; 3 4 2; 2 3 1];
B = [1 0 2;1 0 14; 0 1 11; 1 1 10];
C = zeros(size(A));
% find indexes of non zero locations
B_logi = B ~=0;
C(B_logi) = A(B_logi) ./ B(B_logi);
% display result
C
C = 4×3
1.0000 0 0 1.0000 0 0 0 4.0000 0.1818 2.0000 3.0000 0.1000
  3 件のコメント
Karim
Karim 2022 年 6 月 27 日
A = [1 0 0; 1 1 0; 3 4 2; 2 3 1];
B = [1 0 2;1 0 14; 0 1 11; 1 1 10];
C = zeros(size(A));
% find indexes of non zero locations
B_logi = B ~=0;
C(B_logi) = A(B_logi) ./ B(B_logi);
% find indexes of non zero locations
C_logi = C ~=0;
C_nonzero = C(C_logi)
C_nonzero = 7×1
1.0000 1.0000 2.0000 4.0000 3.0000 0.1818 0.1000
histfit( C_nonzero(:) )
Jialin Men
Jialin Men 2022 年 6 月 27 日
Thank you so much.
It really help me solve a big problem.
Many Many Thousands Thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by