# Creating a vector made of three different unit vectors

10 views (last 30 days)
alexandra ligeti on 28 Jun 2021
Commented: Mathieu NOE on 5 Jul 2021
Good afternoon,
I have three different unit vectors (I, J, K) composed of [Ix, Iy, Iz], [Jx, Jy, Jz], and lastly [Kx, Ky, Kz]. Each in dimension of 6349X3. I would like to create a vector P1 in the from that P1 = [Ix Jx Kx
Iy Jy Ky
Iz Jz Kz ]
And P2 in the same format of P1 but has different values of I, J, K
I need to create this P1 and P2 vector in order to calculate the rotation matrix, [R] = [P2]T[P1]
How do I create P1 and P2 so that the P1 and P2 is in dimesion 3X3 and cycles through the first index to the last calculating the rotation vector for each index?
I tried creating a for loop, however I landed up getting a vector in dimesnions 6349X9.
Please let me know if you require any data, workspace or code for reference.
Thank you in advance

Mathieu NOE on 28 Jun 2021
hello Alexandra
see demo code below ; the matrixes P and R have size 3 x 3 x 6349 (iterations are in 3rd index)
% A = [Ix, Iy, Iz] dimension of 6349X3
% B = [Jx, Jy, Jz] dimension of 6349X3
% C = [Kx, Ky, Kz] dimension of 6349X3
% P1 = [Ix Jx Kx
% Iy Jy Ky
% Iz Jz Kz ];
% so P1 = [A' B' C'];
m = 6349;
n = 3;
A = rand(m,n);
B = rand(m,n);
C = rand(m,n);
T = eye(n,n);
for ci = 1:m
P1 = [A(ci,:)' B(ci,:)' C(ci,:)'];
P2 = P1; % dummy code => change the P2 computation as needed
R(:,:,ci) = P2*T*P1;
end
Mathieu NOE on 5 Jul 2021
hello again
so the NaN are generated here , because with the actual way to zero pad the data , these lines of code will do zero divided by zero , and this gives NaN
%% Unit vectors
junit_REFvec_4 = newSvec_4./(Mag_newSvec_4+eps);
iunit_REFvec_3 = newSvec_3./(Mag_newSvec_3+eps);
kunit_REFvec_1 = -newSvec_1./(Mag_newSvec_1+eps);
junit_Dvec_4_5 = newDvec_4_5./(Mag_newDvec_4_5+eps);
iunit_Dvec_3_5 = newDvec_3_5./(Mag_newDvec_3_5+eps);
kunit_Dvec_1_5 = -newDvec_1_5./(Mag_newDvec_1_5+eps);
one way is to add at the denominator a tiny non zero value (like eps = 2.2204e-16) so that the division gives zero instead of NaN
one other alternative is to zero padd the data after you have done the divisions , but that requires a bit reworking the entire code.
hope it helped
also I did modify a bit the end of your code as I prefer this way of managing the time vectors (and not manually put the amount of samples) :
%% Quick check of data
figure
close all
fs_vicon = 100;
fs_sensor = 50; %approximately 50HZ
dt_v = 1/fs_vicon;
dt_s = 1/fs_sensor;
% time_v = (0:dt_v:4860*dt_v);%+0.54;
time_v = (0:Vicmax-1)'*dt_v;;%+0.54;
% time_v = time_v';
% time_s = 0:dt_s:1996*dt_s; %MSmax
time_s = (0:MSmax-1)'*dt_s; %MSmax
% time_s = time_s';
plot(time_v,(alpha_5)');
hold on
plot(time_s, MS_5_Alignment);
hold on

### Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by