Making two 3D vectors the same length
1 view (last 30 days)
alexandra ligeti on 9 Jun 2021
I have two vectors, vector_static and vector_dynamic which both contain x, y ,z coordinates, however vector static is 421X3 and vector dynamic is 489x3 in length. How do I pad vector static with zeros at the end so that both vectors are the same dimension? I need them to be the same dimension to carry out the dot product on them, so to calculate the angle between. The data is also only changing in the y and z planes so I would only need to dot these components, however, any help regarding changing the dimesions of the matrix would be greatly appreciated.
Thanks in advance
The code I have so far is:
%%Points on pendulum
%Point 1 S =Source Static
point_1_S = [SourceSx, SourceSy, SourceSz];
%Point 1 D =Source Dynamic
point_1_D = [SourceDx, SourceDy, SourceDz];
%Point 2 S = Fusion static
point_2_S = [FusionSx, FusionSy, FusionSz];
%Point 2 D = Fusion dynamic
point_2_D = [FusionDx, FusionDy, FusionDz];
% Creating a vector between the fusion and source sensors
Vector_static = point_2_S - point_1_S;
Vector_dynamic = point_2_D - point_1_D;
%Calculate the angle between vectors
A = cat(4,Vector_static); % Combine the three components in the 4th dimension
B = cat(4,Vector_dynamic); % Ditto
C = cross(A,B,4); % Take the cross products there.
ang_SD = atan2(sqrt(dot(C,C,4)),dot(A,B,4));
Scott MacKenzie on 9 Jun 2021
Edited: Scott MacKenzie on 9 Jun 2021
Given that your question is...
How do I pad vector static with zeros at the end so that both vectors are the same dimension?
then here is a simply way to achieve this:
% test matrices, sized as per question
vStatic = rand(421,3);
vDynamic = rand(489,3);
% pad with zeros so both are same size
vStatic(end:size(vDynamic,1),:) = 0;