How to avoid loop for computing the Center of Mass of many users?

1 visualizzazione (ultimi 30 giorni)
Hello I have to compute the center of mass for different users. For each users I know all the place he visited during the time. So I have a 3D array: ID, Lat, Long.
8017398 41.5271000000000 -5.68340000000000
7976899 41.6775000000000 1.75920000000000
7976899 41.3368000000000 2.12450000000000
8038623 41.0100000000000 -5.67490000000000
8038623 40.9371000000000 -5.69310000000000
8038623 40.9371000000000 -5.69310000000000
8063460 37.6759000000000 -0.956600000000000
1045507 36.4442000000000 -6.20540000000000
8602133 41.6698000000000 -4.68180000000000
153615 39.7778000000000 3.03880000000000
I wrote the function
function Xm = CenterOfMass(lat,lon)
Xm(1,:) = cos(lat) .* cos(lon);
Xm(2,:) = cos(lat) .* sin(lon);
Xm(3,:) = sin(lat);
X = sum(Xm(1,:))/size(Xm,2);
Y = sum(Xm(2,:))/size(Xm,2);
Z = sum(Xm(3,:))/size(Xm,2);
Lon = atan2(Y,X);
Hyp = sqrt(X^2 + Y^2);
Lat = atan2(Z, Hyp);
clear Xm
Xm(1)=Lat*(180/pi);
Xm(2)=Lon*(180/pi);
Because I have a very large number of users, How can I compute the center of mass for each users without doing a loop?

Risposte (1)

Abhishek Pandey
Abhishek Pandey il 17 Lug 2015
Hello Emanuele,
I assume that you have a set of latitudes and longitudes for different users, and for every user, you are calling the “CenterOfMass” function. I believe that the “ parfor ” function might be useful in this case. It is used to execute loop iterations in parallel, and since your function for different users would not be interdependent, it might prove helpful. Note that “parfor” requires Parallel Computing Toolbox (PCT).
Alternatively, if the number of latitude-longitude that you have for every user is same, you could also consider using “reshape” function to convert your 3D data into 2D first. You can visualize it as reducing one dimension from a 3D matrix. The data would be stored in a 2D matrix by stacking the elements one after other. For example, consider a matrix V of size MxNxP, you can convert V into V2 of size MNxP by using “reshape” as follows:
V2 = reshape(V, M*N, P);
Performing calculations on a 2D matrix would be much faster than doing it inside a ‘for’ loop.
I hope this helps!
- Abhishek

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by