How to make a for loop with matrices using n?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Cassandra Haynes
am 22 Mär. 2019
Kommentiert: Cassandra Haynes
am 22 Mär. 2019
I am trying to make a matrix that is easy for me to change it's dimension while it's overall appearance stays the same. However, matlab does not seem to like the k=1:n part of my code below. Any tips on how to fix it without putting 10 instead of n? I do not get any error no matter how long I let the program run; the matrix simply stays a zero matrix.
n=10;
A=zeros(n,n);
n=10;
for k=1:n
A(k,k)=2;
end
for k=1:(n-1)
A(k,k+1)=-1;
end
for k=1:(n-1)
A(k+1,k)=-1;
end
A(1,1)=1;
0 Kommentare
Akzeptierte Antwort
Stephen23
am 22 Mär. 2019
Bearbeitet: Stephen23
am 22 Mär. 2019
>> n = 10;
>> A = toeplitz([2,-1,zeros(1,n-2)])
A =
2 -1 0 0 0 0 0 0 0 0
-1 2 -1 0 0 0 0 0 0 0
0 -1 2 -1 0 0 0 0 0 0
0 0 -1 2 -1 0 0 0 0 0
0 0 0 -1 2 -1 0 0 0 0
0 0 0 0 -1 2 -1 0 0 0
0 0 0 0 0 -1 2 -1 0 0
0 0 0 0 0 0 -1 2 -1 0
0 0 0 0 0 0 0 -1 2 -1
0 0 0 0 0 0 0 0 -1 2
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!