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.
3 次查看(过去 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
0 个评论
采纳的回答
Giridharan Kumaravelu
2018-7-23
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.
Note: {}Code button at the top of the editor is a useful tool to type a clean code.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!