我在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
如果你想要替换值,因为我已经包含了部分程序,这将是如下:
C.(sprintf('Cnew')):
78
而且,对于a.F
,它如下:
78 82 84 80
80 84 86 82
82 87 88 85
82 87 89 86
对于equalize_dimension(x,y)
函数,它如下:
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
而且,对于distance(x,y)
函数,它如下:
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
运行程序时,出现以下错误:
??? Index exceeds matrix dimensions.
Error in ==> fs at 36
distance_new = distance(d1.x, d1.y);
为什么?
感谢。
答案 0 :(得分:3)
首先,在行distance_new = distance(d1.x, d1.y);
之前停在调试器中
并输入
>> which distance
我怀疑你会得到输出
距离是一个变量。
意味着您使用具有相同名称的变量覆盖函数distance
。
其次,在函数distance
中,i
和j
上嵌套循环的原因是什么?您没有使用这些变量,无论嵌套循环如何,都会计算summation
另请尝试not to use i
and j
as variables。