Filter löschen
Filter löschen

Index exceeds the number of array elements(3)

3 Ansichten (letzte 30 Tage)
Danielle sal
Danielle sal am 16 Apr. 2021
Kommentiert: Andrew Newell am 16 Apr. 2021
For a matrix
A=[4 1 3 1;1 4 1 3;3 1 4 1;1 3 1 4]
[L,P,D]=eigen(A);
I have a function:
function [L,P,D]=eigen(A)
format
[~,n]=size(A);
P=[];
D=[];
L=eig(A);
L=transpose(L);
L=real(L);
L=sort(L);
for i= 1:n-1
Temp1 = L(i);
Temp2 = L(i+1);
if closetozeroroundoff(Temp1-Temp2, 7)== 0
L(1,i+1) = L(1, i);
end
end
if rank(L) ~= n
L = closetozeroroundoff(L,7);
end
fprintf('all eigenvalues of A are\n')
display(L)
M = unique(L);
M = transpose(M);
display(M)
m = groupcounts(transpose(L));
display(m)
for i = 1:n
fprintf('eigenvalue %d has multiplicity %i\n',M(i),m(i))
end
and I get the error that Index exceeds the number of array elements(3) even though it prints each eigenvalue with its multiplicity. Any ideas on how to fix it?
M is the eigenvalues that are unique
L is the list of all the eigenvalues (even the repeated ones)
m is the number of times each value in L appears in M
  2 Kommentare
Andrew Newell
Andrew Newell am 16 Apr. 2021
Can you provide the code for ?
Andrew Newell
Andrew Newell am 16 Apr. 2021
And when does the error message occur?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cris LaPierre
Cris LaPierre am 16 Apr. 2021
We can't run your code. We need your closetozeroroundoff function.
However, the error message isn't about whether or not there are 3 eigen values. It is that you are trying to index a variable that only has 3 values with an index that is greater than 3.
% Create an array with 3 elements
num = [1,2,3];
% Works
num(2)
ans = 2
% Error because 5>length(num)
num(5)
Index exceeds the number of array elements (3).

Kategorien

Mehr zu Linear Algebra finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by