How to fix the error (Index exceeds matrix dimensions)?

1 Ansicht (letzte 30 Tage)
Omar B.
Omar B. am 19 Sep. 2019
Kommentiert: Omar B. am 19 Sep. 2019
could you please check my code. I got the following error
Index exceeds matrix dimensionshow .
Error in NnNonSymmetricLanczos (line 17)
r=A*V(:,j)-alpha*V(:,j)-gamma*V(:,j-1);
function [alpha,beta,gamma,T] = alg(A,v,n)
n=4;
c = ones(n,1);
t = ones(1,n)./(1:n);
A = toeplitz(c,t);
v = randn(n,1);
V=v; W=v/norm(v)^2;
beta=0; gamma=0;
alpha=W(:,1)'*A*V(:,1); T(1,1)=alpha;
for j=1:n
if j==1
r=A*V(:,j)-alpha*V(:,j);
p=A'*W(:,j)-alpha*W(:,j);
else
r=A*V(:,j)-alpha*V(:,j)-gamma*V(:,j-1);
p=A'*W(:,j)-alpha*W(:,j)-beta*W(:,j-1);
end
beta=norm(r);
gamma=r'*p/beta;
end
T(j+1,j)=beta;
T(j,j+1)=gamma;
V=[V,r/beta];
W=[W,p/gamma];
alpha=W(:,j+1)'*A*V(:,j+1);
T(j+1,j+1)=alpha;
end

Akzeptierte Antwort

Matt J
Matt J am 19 Sep. 2019
V and W only have a single column, but you are indexing them V(:,j) and W(:,j) at j>1.
  3 Kommentare
Matt J
Matt J am 19 Sep. 2019
Bearbeitet: Matt J am 19 Sep. 2019
You currently have these two lines creating a one-column matrix, V.
v = randn(n,1);
V=v;
If V should have n columns, then you must change something here.
Omar B.
Omar B. am 19 Sep. 2019
Thank you so much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by