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 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Giridharan Kumaravelu
Giridharan Kumaravelu am 23 Jul. 2018
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.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by