フィルターのクリア

Scan for elements of matrix, to limit them in an interval, and change them.

1 回表示 (過去 30 日間)
Isai Fernandez
Isai Fernandez 2018 年 4 月 15 日
コメント済み: Stephen23 2018 年 4 月 15 日
I am learning to write my programs in Matlab.
I want to limit the value of the matrix elements for some interval, and if they don't accomplish the condition, change them.
For example, for a matrix:
A = |a_11 a_12|
|a_21 a_22|
Limit the values of a_ii for an interval:
-m >= a_ii <= +m.
In my code, the values of the matrix come from other calculation, but I want to limit the values of them; in any case, if a_ii is greater of +m, so change this element of the matrix for +m, the same for -m, but if they are within the limits, let them with its value.
I don't know the size of the matrix, because it depends on preview calculation. Indeed, my matrix always will be a column matrix.
P.D. Sorry for my poor english.

採用された回答

Walter Roberson
Walter Roberson 2018 年 4 月 15 日
a_ii = min( +m, max( -m, a_ii) );
max(-m, a_ii) takes the maximum of -m and a_ii, which is going to be -m if a_ii < -m, but otherwise would be a_ii . So now we have put a lower bound on a_ii . Then min( +m, that result) is going to be the minimum of +m and the result, which is going to be +m if the result exceeds +m and otherwise would be unchanged. So by the end of the min() we have a value that is constrained to -m to +m
  3 件のコメント
Walter Roberson
Walter Roberson 2018 年 4 月 15 日
In the above code, a_ii can be a matrix of all of the elements you wish to limit in this way. You can use this code even for the elements that might be already in range -- no need to select only the ones out of range first.
Stephen23
Stephen23 2018 年 4 月 15 日
"Do you mean, one by one?"
No, you don't need any loops or "one-by-one" operations. You can use this method with the entire array at once: simply supply the limit values as scalars (if they are the same for all array elements) or as arrays of the same size.

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

その他の回答 (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