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]

DH am 26 Jul. 2024
Letzte Aktivität Antwort von David am 8 Aug. 2024

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
David
David am 8 Aug. 2024
correct `disp` into `disp(a)`
aasim Azooz
aasim Azooz am 26 Jul. 2024
nothing wrong. Just change disp to disp(a)
Christian Schröder
Christian Schröder am 26 Jul. 2024
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.