MATLAB error 'index exceeds matrix dimensions'

hello all
i want to run this function but i got index exceeds, i checked my loops but i could not saw the error.
where vn=vs=1 and M=100 and th1=1000 and Dr1=D1=0
function D=distortion(vn,vs,M,th1,x,y,Dr1,D1);
for i=1:M;
for j=1:M;
if i~=j
d1(i,j)=sqrt(((y(j)-y(i))^2+(x(j)-x(i))^2));
Dr1=exp(-d1(i,j)/th1)+Dr1;
end
end
end
for i=1:M
ds1(i)=sqrt(((y(j)-0)^2+(x(j)-0)^2));% point source at 0 0
cor2=exp(-ds1(i)/th1);
D1=cor2+D1;
end
D=(vs^2)-((vs^4)/(M*((vs^2)+(vn^2))))*(2*(D1-1))+((vs^6)/((M^2)*((vs^2)+(vn^2)))*Dr1);
end

답변 (1개)

Image Analyst
Image Analyst 2016년 2월 4일

1 개 추천

This worked for me.
function test1
vn=1
vs=1 ;
M=100 ;
th1=1000;
Dr1=0;
D1=0
y = rand(1,M);
x = rand(1,M);
D=distortion(vn,vs,M,th1,x,y,Dr1,D1)
end
function D=distortion(vn,vs,M,th1,x,y,Dr1,D1)
for i=1:M;
for j=1:M;
if i~=j
d1(i,j)=sqrt(((y(j)-y(i))^2+(x(j)-x(i))^2));
Dr1=exp(-d1(i,j)/th1)+Dr1;
end
end
end
for i=1:M
ds1(i)=sqrt(((y(j)-0)^2+(x(j)-0)^2));% point source at 0 0
cor2=exp(-ds1(i)/th1);
D1=cor2+D1;
end
D=(vs^2)-((vs^4)/(M*((vs^2)+(vn^2))))*(2*(D1-1))+((vs^6)/((M^2)*((vs^2)+(vn^2)))*Dr1);
end
What did you pass in for X and Y?

댓글 수: 16

ali
ali 2016년 2월 4일
,x=rand(1,100); y=rand(1,100);
ali
ali 2016년 2월 4일
i just run it now but same error
Which line did you receive the error on?
ali
ali 2016년 2월 4일
편집: Walter Roberson 2016년 2월 4일
when i run this thing alone i got error
vn=1
vs=1 ;
M=100 ;
th1=1000;
Dr1=0;
D1=0
y = rand(1,M);
x = rand(1,M);
D=distortion(vn,vs,M,th1,x,y,Dr1,D1)
but when i run this
function test1
vn=1
vs=1 ;
M=100 ;
th1=1000;
Dr1=0;
D1=0
y = rand(1,M);
x = rand(1,M);
D=distortion(vn,vs,M,th1,x,y,Dr1,D1)
end
there is no error will be
and i want the first one
We need the error message, everything in red.
ali
ali 2016년 2월 4일
when i call this function alone i did not get any error but when i call it in my main program with other functions i got index issues
We need the error message that you do get.
My guess is that you defined a variable named "distortion", and that is interfering with your ability to call the function named "distortion".
Is there something about "We need the error message, everything in red." that you don't understand? Help us to help you.
ali
ali 2016년 2월 5일
i got this red error .. Index exceeds matrix dimensions.. all my other variables are independent from this function
ali
ali 2016년 2월 5일
편집: ali 2016년 2월 5일
when i just use this prog alone in my main program no error will appear but when i use the function i got matrix index issues
for i=1:M;
for j=1:M;
if i~=j
d1(i,j)=sqrt(((y(j)-y(i))^2+(x(j)-x(i))^2));
Dr1=exp(-d1(i,j)/th1)+Dr1;
end
end
end
for i=1:M
ds1(i)=sqrt(((y(j)-0)^2+(x(j)-0)^2));% point source at 0 0
cor2=exp(-ds1(i)/th1);
D1=cor2+D1;
end
D=(vs^2)-((vs^4)/(M*((vs^2)+(vn^2))))*(2*(D1-1))+((vs^6)/((M^2)*((vs^2)+(vn^2)))*Dr1);
We need a screen capture of the error message. Do not just tell us that it happens, we need to SEE it.
ali
ali 2016년 2월 5일
Look at your comment about here you apply VQ function. Look at the next line of code. What variable names have you used on the left hand side of the assignment?
ali
ali 2016년 2월 5일
yes i used distortion variable.. its worked now i changed it .... thxxxx too much
You see the importance of showing us the actual error instead of just saying that you get an error somewhere?
ali
ali 2016년 2월 5일
yes u r right

이 질문은 마감되었습니다.

태그

아직 태그를 입력하지 않았습니다.

질문:

ali
2016년 2월 4일

마감:

2021년 8월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by