Struct data assignment has error

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 commentaires

Adam
Adam le 16 Déc 2016
What is 'data'? Or rather, what size is it? c is a 1x4 vector so if data is not a compatible size then you will get this error.
data needs to be of size nx4 to work in that bsxfun operation.
shawin
shawin le 16 Déc 2016
Modifié(e) : shawin le 16 Déc 2016
@Adam The data is 100x2 , i can not change data, but I can change the c = from 1x4 to 2x2, I tried reshape pop(i).position but does not work ??
Adam
Adam le 16 Déc 2016
What do you actually want to achieve, conceptually? If you start with 100x2 data and you are trying to subtract a 1x4 vector what would you expect to be result of this operation? Sure you can reshape 1x4 into 2x2 and get the bsxfun syntax to not return an error, but is that the operation you actually want to perform?
It's no use just saying that something "does not work" though. You have to say what doesn't work if you want help. "does not work" can mean anything from a variety of syntax errors to just getting a mathematically wrong answer.
shawin
shawin le 16 Déc 2016
Modifié(e) : shawin le 16 Déc 2016
@Adam,the data is 2 Dimension and it is fixed. now we have c : is 1x4 and the only option that i have is convert 1x4 to 2x2 because this is the nature of the algorithm , my issue is how can i produce the pop(i).position= 2x2 ? instead of 1x4.
i have put the error : Non-singleton dimensions of the two input arrays must match each other. Error in distances (line 8) z = bsxfun(@minus,x, c(i,:));
pop(i).Position= reshape( unifrnd(2,2,4), [2 2] );

Connectez-vous pour commenter.

Réponses (1)

KSSV
KSSV le 16 Déc 2016
Modifié(e) : KSSV le 16 Déc 2016

0 votes

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 commentaires

shawin
shawin le 16 Déc 2016
Modifié(e) : shawin le 16 Déc 2016
@ KSSV Yes , I just put (x= data,pop(i).Position=c) for explanation. sorry ssd is : ssd(:, i) = sqrt(sum(z.^2, 2));
KSSV
KSSV le 16 Déc 2016
Consider the other comments....you have to change your function distances

Connectez-vous pour commenter.

Catégories

Question posée :

le 16 Déc 2016

Commenté :

le 16 Déc 2016

Community Treasure Hunt

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

Start Hunting!

Translated by