Mod or Modulus operator in MATLAB

288 ビュー (過去 30 日間)
Sankar Ram T
Sankar Ram T 2016 年 2 月 2 日
回答済み: John D'Errico 2024 年 2 月 9 日
I want to perform a mod operation on a number.
|10| = 10
|-10| = 10
Basically it returns the positive value of the input. All I could find is the sign() operator which returns 1,-1 or 0. Is there a operator to return the positive value of input?
  1 件のコメント
Antonio Cedillo Hernandez
Antonio Cedillo Hernandez 2021 年 4 月 3 日
編集済み: Antonio Cedillo Hernandez 2021 年 4 月 3 日
abs(10) = 10
abs(-10) = 10

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

回答 (2 件)

Matthew Sisson
Matthew Sisson 2024 年 2 月 9 日
I think you are looking for the absolute value function:
https://www.mathworks.com/help/matlab/ref/abs.html

John D'Errico
John D'Errico 2024 年 2 月 9 日
Be careful, as if you look for mod or modulus, you will often see the functions mod or rem, which essentially compute a remainder after division. My guess is, you were searching for the wrong thing by looking for mod or modulus.
You are asking about the absolute value function. This is pretty standard in most computer languages I can think of as the function named abs. So
abs([-10 10])
ans = 1x2
10 10
Could you have computed that using the sign function? Well, yes. in terms of real numbers, we would have the identity in MATLAB
abs(x) == x .* sign(x)
Effectively, that just switches the sign of the number when it is negative.
But you should also recognize that when applied to complex numbers, abs does more than just multiply by -1 as needed.
Anyway, it is abs that you are asking about.

カテゴリ

Help Center および File ExchangeNumbers and Precision についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by