Recursive HYPOT Function for Multiple Inputs

バージョン 1.2.0.0 (1.52 KB) 作成者: Drew Compston
Extension of the built-in MATLAB function hypot for multiple inputs.
ダウンロード: 215
更新 2012/4/25

ライセンスの表示

MATLAB provides a function called hypot to calculate sqrt(abs(A).^2 + abs(B).^2) in a robust way to avoid underflow and overflow (see http://www.mathworks.com/help/techdoc/ref/hypot.html and http://en.wikipedia.org/wiki/Hypot). Frequently, though, I need to make this calculation with more than just two inputs A and B. Say, to compute the magnitude of a vector with three components (x, y, z). So I wrote this function to recursively call MATLAB's hypot function for multiple inputs:

Usage: Y = HYPOTR(X) or Y = HYPOTR(X1, X2, X3, ...)

This function works the same as the built-in MATLAB function hypot but allows for more than two inputs (or a single array) via recursion. The output Y is:

Y = sqrt(sum(X(:).^2)) (for single array X input)
Y = sqrt(X1.^2 + X2.^2 + X3.^2 + ...) (for arrays X1, X2, X3, ... input)

But the computation is done in a more robust way to avoid overflow and underflow (see HYPOT). Note that when multiple arrays are input, they must all be the same size (but any or all of them can also be scalars).

See also: HYPOT.

引用

Drew Compston (2024). Recursive HYPOT Function for Multiple Inputs (https://www.mathworks.com/matlabcentral/fileexchange/35403-recursive-hypot-function-for-multiple-inputs), MATLAB Central File Exchange. 取得済み .

MATLAB リリースの互換性
作成: R2007b
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linux
カテゴリ
Help Center および MATLAB AnswersGeneral Physics についてさらに検索
タグ タグを追加

Community Treasure Hunt

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

Start Hunting!
バージョン 公開済み リリース ノート
1.2.0.0

Greatly simplified the code.

1.1.0.0

Replaced iscolumn, which was not introduced until R2010b, with size(A, 2) == 1, which should make this compatible with at least R2007b and later.

1.0.0.0