Can someone tell me what's wrong with this code? This is a problem from Cody. For example, when n=3, the output should be the matrix [1 2 2 3 3 3]
function ans = your_fcn_name(n)
n;
j=sum(1:n);
a=zeros(1,j);
for i=1:n
a(1,((sum(1:(i-1))+1)):(sum(1:(i-1))+i))=i.*ones(1,i);
end
disp
3 Comments
Time Descendingcorrect `disp` into `disp(a)`
nothing wrong. Just change disp to disp(a)
There's nothing wrong with it, as such, but your function's output argument is called ans, not a. If you change that to a, it should work.
Two more observations: 1) n; doesn't do anything, and 2) the final disp without an argument will give you an error.
Sign in to participate