Math problem for code

2 visualizaciones (últimos 30 días)
Mahmoud Sami
Mahmoud Sami el 2 de Abr. de 2018
Respondida: njj1 el 2 de Abr. de 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)

Respuesta aceptada

njj1
njj1 el 2 de Abr. de 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.

Más respuestas (0)

Categorías

Más información sobre Debugging and Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by