View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Recursive HYPOT Function for Multiple Inputs

Be the first to rate this file! 2 Downloads (last 30 days) File Size: 1.52 KB File ID: #35403 Version: 1.2

Recursive HYPOT Function for Multiple Inputs


Drew Compston (view profile)


01 Mar 2012 (Updated )

Extension of the built-in MATLAB function hypot for multiple inputs.

| Watch this File

File Information

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 and 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.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
08 Mar 2012 1.1

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

25 Apr 2012 1.2

Greatly simplified the code.

Contact us