List of xyz coordinates for two vectors that continuously change through time, how to find angle between the two vectors as time passes and their coordinates change?

4 次查看(过去 30 天)
Hi,
I have an excel file with a list of coordinates. I am only using columns [3:8]. These columns represent two points and their x,y,z coordinates respectively. The list is 421 rows long, below is a small section of the list to provide an example.
time Source x source y source z fusion x fusion x fusion z
1 0 23.6941000000000 579.700000000000 1223.33000000000 49.4259000000000 581.346000000000 104.230000000000
2 0 23.6883000000000 579.718000000000 1223.31000000000 49.4150000000000 581.310000000000 104.208000000000
3 0 23.6925000000000 579.732000000000 1223.30000000000 49.4202000000000 581.265000000000 104.198000000000
4 0 23.6919000000000 579.741000000000 1223.32000000000 49.4326000000000 581.195000000000 104.212000000000
I have split up the csv file to create point1 (Source x, y, z) which contains entries 3-5 from the list and point 2 (fusion x, y, z) which contains entries 6-8 from the list.
I am trying to calculate the angles in all planes of space between point 1 and point 2 as they change coordinates as time passes.
I am really stuck trying to make a vector that continously updates as the coordinates change. I have tried doing a for loop but I cant get it to work, because I cant create these vectors I cannot calculate the angles between the vectors as time passes.
The vector is from point 1 to point 2 and it constantly changes poistion as time passes (Column 1).
At the moment I have;
ViconStatic = readmatrix('NewStaticTestSensor.csv');
MotionSenseStatic = readmatrix('67CT_log_07062021_14-37-34');
%% Import vicon coordinates
%Time
time = linspace(1,421,421)./1000;
time_Sec = reshape(time,421,1);
% Source sensor coordinates
Sourcex = ViconStatic(1:421,3);
Sourcey = ViconStatic(1:421,4);
Sourcez = ViconStatic(1:421,5);
% Fusion sensor coordinates
Fusionx = ViconStatic(1:421,6);
Fusiony = ViconStatic(1:421,7);
Fusionz = ViconStatic(1:421,8);
%% Creating moving vectors
r=1;
for s=0:1:421
p1x(r,1)= Sourcex(s+1,1);
r=r+1;
end
Any help would be greatly appreciated.

回答(1 个)

Animesh
Animesh 2024-2-20
It seems like you are trying to calculate the vector that points from the source point (Source x, y, z) to the fusion point (fusion x, y, z) at each time step, and then find the angles in the 3D space between these vectors as they change over time.
Firstly, the loop to create the vector can be avoided. In MATLAB we can directly subtract matrices. So, we can directly subtract source coordinates from fusion coordinated to get the vectors.
We can do something like this:
% After reading the matrices from “.csv” file
Sourcex = ViconStatic(1:421,3);
Sourcey = ViconStatic(1:421,4);
Sourcez = ViconStatic(1:421,5);
Fusionx = ViconStatic(1:421,6);
Fusiony = ViconStatic(1:421,7);
Fusionz = ViconStatic(1:421,8);
% Creating vectors from source to fusion
Vectorx = Fusionx - Sourcex;
Vectory = Fusiony - Sourcey;
Vectorz = Fusionz - Sourcez;
Now, once we have the vectors, we can calculate the angles between them using the dot product. The dot product of two vectors A and B is defined as dot(A,B) = |A| * |B| * cos(theta), where theta” is the angle between the vectors, and |A| and |B| are the magnitudes of the vectors A and B respectively.
We can calculate it as follows:
% Calculate the magnitude of each vector
Magnitude = sqrt(Vectorx.^2 + Vectory.^2 + Vectorz.^2);
% Normalize the vectors (make them unit vectors)
UnitVectorx = Vectorx ./ Magnitude;
UnitVectory = Vectory ./ Magnitude;
UnitVectorz = Vectorz ./ Magnitude;
% Calculate the dot product of consecutive vectors
% Initialize the angles array
angles = zeros(420, 1); % There will be one less angle than the number of time points
for i = 1:(length(UnitVectorx)-1)
dot_product = UnitVectorx(i) * UnitVectorx(i+1) + UnitVectory(i) * UnitVectory(i+1) + UnitVectorz(i) * UnitVectorz(i+1);
angles(i) = acos(dot_product);
end
% Convert angles from radians to degrees if needed
angles_degrees = rad2deg(angles);
Here, we are assuming that the angle is being calculated between the vector at one time point and the vector at next time point. If the angle was to be calculated with respect to a fixed reference vector, we would have to adjust the dot product accordingly.

类别

Help CenterFile Exchange 中查找有关 Matrix Decomposition 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by