power function in matlab

To raise a base to a certain power in MATLAB, you can use the ** operator or the power function.

Using the ** operator:

main.m
% syntax: base ** exponent
5 ** 3     % returns 125
52 chars
3 lines

Using the power function:

main.m
% syntax: power(base, exponent)
power(5, 3)   % returns 125
60 chars
3 lines

Both the ** operator and the power function can also accept arrays as inputs.

main.m
% example with arrays
base = [2, 3, 4];
exponent = [3, 2, 3];

% using **
result = base ** exponent   % returns [8, 9, 64]

% using power function
result = power(base, exponent)   % returns [8, 9, 64]
201 chars
10 lines

gistlibby LogSnag