matlabmatlab-compiler

Matlab programming dealing with matrix


I am trying out one of the matlab programming question.

Question:

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

My Code:

function H = hulk(v)
H = [v; v.^2; v.^3];
size(H) = (n,3);
end

When I test my code using A = hulk(1:3), it throws an error on console.

Your function made an error for argument(s) 0

Am I doing something incorrect? Have I missed anything?


Solution

  • Remove the line size(H) = (n,3); and add the line H = H';

    Final code should be as follows

    function H = hulk(v)
        H = [v; v.^2; v.^3];
        H = H';
    end
    

    Your code giving error in matlab editor on the size(H) = (n,3); line enter image description here

    That's why you should use the matlabeditor itself