Search Program on this blog

Tuesday 18 August 2015

Write a function called hulk that takes a row vector v as an input and returns a matrix H whose first column consist of the elements of v, whose second column consists of the squares of the elements of v, and whose third column consists of the cubes of the elements v. For example, if you call the function likes this, A = hulk(1:3) , then A will be [ 1 1 1; 2 4 8; 3 9 27 ].

function H = hulk(v)
%{
Input v is a row vector
Output H is a matrix
Example:
    A = hulk(1:3)
%}
H = [v; v.^2; v.^3]'; %column 1, 2, 3 are elements of v, squares and cubes respectively

No comments:

Post a Comment