I am a MATLAB beginner, I have a function that generates 100 X and Y coordinates for 30 users. X and Y are 30 by 100 matrices. How do I generate the distance between a user and the other 29 users, for each of the 30 users.

1 次查看(过去 30 天)
NoUsers = 30; s = 100; alpha = 1.5; [x,y] = levywalkfunc(alpha,s); for m = 1:NoUsers [x(m,:),y(m,:)] = levywalkfunc(alpha,s); end for m = 1:NoUsers activeset = setdiff(1:NoUsers,m); for j = activeset for t= 1:s D(m, s) = sqrt((x(m,s) - x(j,s))^2+(y(m,s) - y(j,s))^2); end end

采纳的回答

Giridharan Kumaravelu
for m = 1:NoUsers
for j = 1:NoUsers
for t = 1:s
D(m,j,t) = sqrt((x(m,t) - x(j,t))^2+(y(m,t) - y(j,t))^2);
end
end
end
I believe this is what you were trying to do, which outputs a three dimensional vector of distances of the 100 points between users. This could be very inefficient way doing in MATLAB. The power of MATLAB lies in the efficient built-in functions for matrix operations which can run faster than looping statements.
You could try the dist () function.
Note: {}Code button at the top of the editor is a useful tool to type a clean code.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by