Divide a scalar by all non-zero values in a matrix row and input results into a new matrix

8 ビュー (過去 30 日間)
Robert Demyanovich
Robert Demyanovich 2022 年 1 月 3 日
コメント済み: dpb 2022 年 1 月 3 日
I have a matrix cA. I want to input values into a matrix A that are a scalar (1E-14) divided by each element in a row of cA that is not equal to zero.
I have so far:
A = (1E-14)./cA(i,:);
but this uses every column in the ith row. Some of the cell values in the columns are zero so the result is infinity in some cells/elements of A. I would only like to input a value into an element of A if cA(i,j) > 0. Alternatively, if the result is infinity, then just input a zero into that element of A.
  4 件のコメント
Meg Noah
Meg Noah 2022 年 1 月 3 日
Another Trick:
verySmallNumber = 1.0e-32;
denom = cA;
denom(abs(denom) < verySmallNumber & denom > 0) = verySmallNumber;
denom(abs(denom) < verySmallNumber & denom < 0) = -verySmallNumber;
A=MAGICCONSTANT./denom;
dpb
dpb 2022 年 1 月 3 日
OBTW,
A(isinf(A)) = 0;
also works by definition that
isinf(-inf) == isinf(+inf)

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

回答 (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