finding the orthogonal vectors for a series of vectors
42 views (last 30 days)
I'm looking for some help to calculate the orthogonal vectors to a series of vectors. I have vector xhat which is (about700000x3) and every line or row of it is the vector that I want to calculate the two orthogonals for. I am aware of the null function however I'm struggling to implement it for the data type type I have. Any help is appriciated.
Bjorn Gustavsson on 22 Jul 2022
This is how I would do it:
nVecs = 12
allVecs = randn(nVecs,3);
for i1 = size(allVecs,1):-1:1
u_test = randn(1,3);
while is_parallel(allVecs(i1,:),u_test) % You'll have to write this test yourself
u_test = randn(1,3);
% now we have a vector u_test that are not parallel to allVec(i1,:)
% First perpendicular vector:
VecPerp1(i1,:) = cross(allVecs(i1,:),u_test);
% then a second perpendicular to both allVecs(i1,:) and VecPerp1(i1,:) is
% the cross-product of those two.
VecPerp2(i1,:) = cross(allVecs(i1,:),VecPerp1(i1,:));
You will have to clean up this snippet a bit with normalizzations etc. Perhaps you also want some other characteristics of the perpendicular vectors - but you can always make some pair of linear combinations or rotations of them afterwards, or select the test-vector u_test a bit differently. This fullfills the conditions you presented.
More Answers (5)
Dyuman Joshi on 22 Jul 2022
Another method will be -
%generate a random vector
%check it is not parallel to original vector
%first perpendicular vector
%second perpendicular vector
%store the vectors as you like
There are some trivial methods as well -
%for example let your vector be [a b c]
%then perpendicular vectors are -
q1 = [-b a 0];
q2 = [-c 0 a];
q3 = [0 c -b];
John D'Errico on 22 Jul 2022
Edited: John D'Errico on 22 Jul 2022
I think you are confused. Really, this is not a difficult problem. You need to understand the linear algebra of it, or you will still be confused. You have an array of size 700000x3, or whatever. For each row of this array, you want to find TWO vectors normal to the corresponding row. Of course, they will not be unique, since we can always rotate them arbitrarily around the axis of the original vector.
Lacking any data from you, I'll create some completely random vectors.
V = randn(700000,3); % My personal set of initial vectors
We will want that set to have unit norm though, as that makes the later computations easier.
n = size(V,1); % how many vectors are there?
Vnormalized = normalize(V,2,'norm'); % scales every row to have unit 2-norm
Next, for each vector, we need to find a new vector that will NEVER be parallel to the vectors in Vnormalized. I'll be tricky here, to insure we will always succeed. The idea will be to find the element with the smallest absolute value in each vector, and modify that element. That will create a new vector that is ABSOLUTELY assured to be not parallel.
[~,ind] = min(abs(Vnormalized),,2);
Q1 = eye(3);
Q1 = normalize(Q1(ind,:) + Vnormalized,2,'norm'); % normalize Q1
But while Q1 is not parallel to V, it is not orthogonal either. We can make that happen now using a dot product, subtracting off the component of Q1 that is still parallel to Vnormalized.
Q1 = Q1 - sum(Q1.*Vnormalized,2).*Vnormalized; % requires R2016b or later
Q1 = normalize(Q1,2,'norm'); % re-normalize Q1
Ok. We now have TWO vectors, Vnormalized and Q1, bioth of which have unit norm, and which are orthogonal. The third is easy. Just use a cross product. And since I've already set up Vnormalized and Q1 to be unit vectors, then so will be the cros product.
Q2 = cross(Vnormalized,Q1);
The resulting rows of Q1 and Q2 are now each unit norm, and they are orthogonal to the corresponding rows of V. For example, consider the first row of V.
M = [Vnormalized(1,:);Q1(1,:);Q2(1,:)]
This next result will be the identity matrix if we were successful, to within floating point trash in the least significant bits.
format long g
And that is as good as we can do.
Bruno Luong on 22 Jul 2022
Edited: Bruno Luong on 22 Jul 2022
If you have recent MATLAB (from 2021b)
% Invent some test data
xhat = rand(10,3);
% N = pagetranspose(N) if prefer 2 row vectors for each page
% Check, each of two columns of N(:,:,k) is orthogonal to xhat(k,:) for all k
xhat(1,:)*N(:,:,1) % almost 0
xhat(end,:)*N(:,:,end) % almost 0
Bruno Luong on 23 Jul 2022
Edited: Bruno Luong on 23 Jul 2022
A direct method for vectors in R^3
a=rand(100,3); % but be non zeros
% Generate b and c orthogonal to a
a2 = a(:,[2 3 1]); a3 = a(:,[3 1 2]);
b = a2-a3;
% Normalize a, b, c if required ...
% Check orthogonality
There is some spectial treatment needs to be applied when a has 3 component identical (parallel to [1,1,1]). But I'll leave this detail out, unless someone requrires it.
David Goodmanson on 25 Jul 2022
Here is another method, somewhat related to Bruno's
a = 2*rand(1000,3)-1;
m1 = tril(ones(3,3)) -triu(ones(3,3))
m2 = [1 1 1]'*[-1 1 -1];
m3 = [1 -1 1]'*[1 1 1];
b = a*m1;
c = (a.*a)*m2 + a.*(a*m3);