Struct data assignment has error
1 次查看(过去 30 天)
显示 更早的评论
am trying to calculate a distance for struct data type. The distance method is:
function ssd=distances(x, c)
m=size(x,1);
ssd = zeros(m, k);
k=size(c,1);
for i= 1:k
z = bsxfun(@minus,x, c(i,:));
ssd(:, i) = sqrt(sum(z.^2, 2));
end
end
I have c which is a random number produced from the formula below:
for i=1:20
pop(i).Position=unifrnd(2,2,4);
pop(i).dist=distances(data,pop(i).Position);
end
I have c which is a random number produced from the formula below:
for i=1:20
pop(i).Position=unifrnd(2,2,4);
pop(i).dist=distances(data,pop(i).Position);
end
x=data is 100x2.
The issue is when I call the distance method it does not accept the passing argument. pop(i).dist=distances(data,pop(i).Position);
the error is :
Non-singleton dimensions of the two input arrays must match each other. Error in distances (line 8) z = bsxfun(@minus,x, c(i,:));
5 个评论
回答(1 个)
KSSV
2016-12-16
编辑:KSSV
2016-12-16
Try
pop(i).dist=distances(data,pop(i).Position);
Instead of
pop(i).dist=distances(x= data,pop(i).Position=c);
Also in the function, distances copy initilaize ssd after getting k. What does this mean?
ssd(:,i) = sqrt(z.^2,2);
sqrt takes only one input. sqrt(z.^2); But it has no meaning, in squaring and finding the square root. Chekc the initilaization of ssd, it will throw error.
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computer Vision with Simulink 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!