필터 지우기
필터 지우기

Index exceeds the number of array elements(3)

조회 수: 3 (최근 30일)
Danielle sal
Danielle sal 2021년 4월 16일
댓글: Andrew Newell 2021년 4월 16일
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
Andrew Newell
Andrew Newell 2021년 4월 16일
Can you provide the code for ?
Andrew Newell
Andrew Newell 2021년 4월 16일
And when does the error message occur?

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

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 4월 16일
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).

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by