Math problem for code

4 vues (au cours des 30 derniers jours)
Mahmoud Sami
Mahmoud Sami le 2 Avr 2018
Réponse apportée : njj1 le 2 Avr 2018
I had a matrix S=n*3 (x,y,z) where n is variable change every run and i want to get another variable L1 to Ln where L=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2) in a matrix contain no of column (n)

Réponse acceptée

njj1
njj1 le 2 Avr 2018
Try using diff(S,1,dim) to get the difference between sequential elements in the matrix S. Each entry in diff(S) is (x_{n} - x_{n-1}, y_{n} - y_{n-1}, z_{n} - z_{n-1}). Then you can square each element of diff(S): (diff(S)).^2. Finally, sum and take the square root: sqrt(sum(diff(S)).^2),dim). In each of these cases, dim is the dimension along which the operation is to take place. So, for the sake of discussion, let's assume S is nx3 (n columns and 3 rows).
diffS = diff(S,1,1);
squaredDiffS = diff.^2;
output = sqrt(sum(squaredDiffS,2));
The output of this code is (n-1)x1, because one column is lost in the diff command.

Plus de réponses (0)

Catégories

En savoir plus sur Numerical Integration and Differentiation dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by