??? Index exceeds matrix dimensions.

조회 수: 9 (최근 30일)
med-sweng
med-sweng 2013년 2월 13일
I have the following code (part of a larger program) in `matlab`:
while(k<n)
C.(sprintf('Cnew')) = C.(sprintf('C%d',k));
d1 = equalize_dimension(a.F, C.(sprintf('Cnew')));
distance_new = distance(d1.x, d1.y);
k = k + 1;
end
If you want to substitute values since I have included part of the program, this would be as follows:
C.(sprintf('Cnew')):
78
And, for `a.F` it is as follows:
78 82 84 80
80 84 86 82
82 87 88 85
82 87 89 86
For the `equalize_dimension(x,y)` function, it is as follows:
function n = equalize_dimension (x,y)
[r1 c1] = size(x);
[r2 c2] = size(y);
if r1<r2
e= r2-r1;
for i=1:e
x(r1+1,1)=0;
r1 = r1 + 1;
end
[r1 c1] = size(x);
n.x =x;
n.y = y;
end
if r1>r2
e = r1-r2;
for i=1:e
y(r2+1,1)=0;
r2 = r2 + 1;
end
[r2 c2] = size(y);
n.x = x;
n.y = y;
end
if c1<c2
e= c2-c1;
for i=1:e
x(1,c1+1)=0;
c1 = c1 + 1;
end
[r1 c1] = size(x);
n.x = x;
n.y = y;
end
if c1>c2
e = c1-c2;
for i=1:e
y(1,c2+1)=0;
c2 = c2 + 1;
end
[r2 c2] = size(y);
n.x = x;
n.y = y;
end
if r1==r2 && c1==c2
n.x = x;
n.y = y;
end
And, for the `distance(x,y)` function, it is as follows:
function m = distance(x,y)
[r c] = size(x);
for i=1:r
for j=1:c
summation = (sum(sum(pdist2(x,y))));
end
end
m=summation;
end
When I run the program, I get the following error:
??? Index exceeds matrix dimensions.
Error in ==> fs at 36
distance_new = distance(d1.x, d1.y);
Why is that?
Thanks.

답변 (1개)

Thorsten
Thorsten 2013년 2월 13일
편집: Thorsten 2013년 2월 13일
Probably you have defined a variable distance that shadows the function distance. You can check that by using
which distance
distance_new = distance(d1.x, d1.y);
You did not ask for it, but I realized that you can simplify your function equalize_dimension considerably
function n = equalize_dimension(x, y)
Z = zeros(max([size(x); size(y)]));
n.x = Z; n.x(1:size(x, 1), 1:size(x, 2)) = x;
n.y = Z; n.y(1:size(y, 1), 1:size(y, 2)) = y;
Further I noted that you run your pdist2 function inside the loop always on x and y that do not change depending on i and j.
And please provide the pdist2 function.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by