MATLAB: Code will not display getting error

MATLAB and Simulink Student Suitematrix manipulation

I am getting an error on line 14 it will not display B. Any suggestions? This is the code:
A=[1 2 3;4 5 6;7 8 9]
[m,n]=size(A);
while j<=n
i=1;
sum1=0;
while i<=m
sum1=sum1+A(i,j);
B(i,j)=sum1;
i=i+1;
end
j=j+1;
end
disp(B)

Best Answer

I could not run your code, because j is not yet defined before you enter the loop. I initialized j to 1, and then it worked (and displayed B):
A=[1 2 3;4 5 6;7 8 9]
[m,n]=size(A);
j=1;
while j<=n
i=1;
sum1=0;
while i<=m
sum1=sum1+A(i,j);
B(i,j)=sum1;
i=i+1;
end
j=j+1;
end
disp(B)
But I don't know if that's exactly what you need.