Cody
Follow


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 on 26 Jul 2024
Latest activity Reply by Richard on 23 Sep 2024 at 6:07

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
Richard
Richard on 23 Sep 2024 at 6:07
If you're facing issues with MATLAB code and need expert assistance, consider reaching out for MATLAB assignment help at www.matlabassignmentexperts.com. Their experts can guide you through debugging and code optimization!
David
David on 8 Aug 2024
correct `disp` into `disp(a)`
aasim Azooz
aasim Azooz on 26 Jul 2024
nothing wrong. Just change disp to disp(a)
Christian Schröder
Christian Schröder on 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.

Tags

No tags entered yet.