# i want to put n and counter in one matrix

조회 수: 1(최근 30일)
yonatan friedman 2020년 12월 12일
답변: Abhishek Gupta 2020년 12월 15일
for n=4:50
a=[];
e = ones(n,1);
A1 = spdiags([-e 0*e -e 4*e -e 0*e -e],-3:3,n,n);
full(A1);
A=full(A1);
b=zeros(n,1);
b(1)=1; b(end)=1;
x=zeros(n,1);
L=tril(A,-1);
U=triu(A,1);
D=diag(A);
B=-1./D.*(L+U);
c=(1./D).*b;
flag=0;
counter=0;
epsilon=10^(-3);
while flag==0
counter=counter+1;
if counter>100000
error('too much')
end
x_new=B*x+c;
if max(abs((x_new-x)./x_new))<epsilon
flag=1;
end
x=x_new;
end
fprintf('%g %g\n',counter,n)
end

댓글을 달려면 로그인하십시오.

### 답변(1개)

Abhishek Gupta 2020년 12월 15일
Hi,
As per my understanding, you want to create a matrix containing 'n' and 'counter' variables. One can do this task by appending a row, containing 'n' and 'counter' variables, at the end of the matrix on every iteration of the loop.
out = []; % output matrix
for n = 4:50
out = [out; [n, counter]]; % append row containing n and counter
end

댓글을 달려면 로그인하십시오.

### 범주

Find more on Get Started with MATLAB in Help Center and File Exchange

R2018b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by