Effacer les filtres
Effacer les filtres

Projecting a vector to another vector

224 vues (au cours des 30 derniers jours)
Victor
Victor le 28 Fév 2011
I would like to project a vector to another vector. You can find more information here:
For example I would like to project vector A to vector B. I have used these tricks but it does not work: Any comment is appreciated.
-----------------------
Solution 1)
A=[-10,10,0];
B=[0,0,1];
C=(dot(A,B)/norm(B)^2)*B
---------------------------------
Solution 2)
A=[-10,10,0];
B=[0,0,1];
CosTheta = dot(A,B)/(norm(A)*norm(B));
ThetaInDegrees = acos(CosTheta)*180/pi;
c=norm(A)*cos(ThetaInDegrees)
-----------------------------
  1 commentaire
Matt Fig
Matt Fig le 2 Mar 2011
You should select a best answer if your question has been answered.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 1 Mar 2011
You have to tell DOT, that it must work on the 2nd dimension. Finally BSXFUN is needed to multiply the [88 x 1] and the [88 x 3] arrays. Unfortunalely NORM replies a matrix norm when operating on a matrix. Either use DNorm2: http://www.mathworks.com/matlabcentral/fileexchange/29035-dnorm2 or create the vector norm manually:
A = repmat([10,10,-10] ,[88,1]);
B = repmat([1,1,-1], [88,1]);
lenC = dot(A, B, 2) ./ sum(B .* B, 2); % EDITED, twice
C = bsxfun(@times, lenC, B)
  4 commentaires
Matt Fig
Matt Fig le 3 Mar 2011
You need another parenthesis on the end of lenC.
Jan
Jan le 4 Mar 2011
Thanks Matt, fixed.

Connectez-vous pour commenter.

Plus de réponses (9)

Paulo Silva
Paulo Silva le 28 Fév 2011
A=[-10,10,0];
B=[0,0,1];
%calculation of the projection of A into B
C=(sum(A.*B)/(norm(B)^2))*B;
%versors of each vector
An=A/norm(A);
Bn=B/norm(B);
Cn=C/norm(C);
%graphic representation
clf
line([0 A(1)],[0 A(2)],[0 A(3)],'LineWidth',10,'Color',[0 0 1])
line([0 B(1)],[0 B(2)],[0 B(3)],'LineWidth',8,'Color',[0 1 0])
line([0 C(1)],[0 C(2)],[0 C(3)],'LineWidth',5,'Color',[1 0 0])
legend('A','B','proj A into B')
xlabel('X')
ylabel('Y')
zlabel('Z')
view(80,10)
  5 commentaires
Paulo Silva
Paulo Silva le 28 Fév 2011
Should be working properly now
Victor
Victor le 1 Mar 2011
Dear Paulo and Matt
Thanks so much for your comments.

Connectez-vous pour commenter.


Jan
Jan le 1 Mar 2011
What exactly does "but it does not work" mean?
Your solution 1:
A = [-10,10,0];
B = [0,0,1];
C = (dot(A,B)/norm(B)^2)*B
This looks ok. If you get C = [0,0,0], the method works. A and B are orthogonal, such that the projection is zero.
Your solution 2: wrong
CosTheta = dot(A,B)/(norm(A)*norm(B));
ThetaInDegrees = acos(CosTheta)*180/pi;
c=norm(A)*cos(ThetaInDegrees)
Now c is a scalar, but you wanted a vector. Converting Theta in degrees is not correct here: COS works win radians. Use COSD for degerees. Improved:
CosTheta = dot(A,B) / (norm(A)*norm(B));
C = norm(A) * CosTheta * B / norm(B);
And as expected: If you insert CosTheta in the 2nd line, you get your solution 1 again.
  2 commentaires
Paulo Silva
Paulo Silva le 1 Mar 2011
I failed somehow to find the function dot and done sum(A.*B) instead :) but the results are the same
Jan
Jan le 1 Mar 2011
sum(A.*B) and A*B' are faster then DOT. But for [1 x 3] vectors this does not matter.

Connectez-vous pour commenter.


Victor
Victor le 1 Mar 2011
Dear all,
Thanks for your brilliant comments.
Let me describe the problem. Actually I have two n*3 matrices that I should project one of them to another one.(I use dlmread to read these files) Every raw of these matrices are components of separate vectors. in another word, first columns are "x" values, second columns are "y" values and third columns are "z" values--> That is the reason why by mistake I selected two perpendicular vectors in my example :)
Lets assume we have these matrices:
AA=[10,10,-10];
A=repmat(AA,[88,1]);% lets assume this is my first matrix
BB=[1,1,-1];
B=repmat(BB,[88,1]); % and for example this is my second matrix that I am going to project the A matrix to it
%CC = (dot(AA,BB)/norm(BB).^2)*BB %in this case it will project the AA matrix to BB matrix
C = (dot(A,B)/norm(B).^2)*B % but for this one it gives the error of "Inner matrix dimensions must agree", that is because (dot(A,B)/norm(B).^2) is a 1*3 matrix and B is a 88*3 matrix or maybe because of another reason because when I change B matrix to BB matrix, still I have the same problem!!
Thanks in advance for your comments and guide lines.

Foday Samura
Foday Samura le 1 Mai 2020
Find the length (or norm) of the vector that is the orthogonal projection of the vector a = [ 1 2 4 ] onto b = [6 10 3 ]. Type an answer that is accurate to 3 decimal places. (For example, if your answer is 4+2/3, you should type 4.667).

fatema hasan
fatema hasan le 13 Déc 2020
  1. Write the complete MATLAB commands to find the unit vector parallel to the projection of #5

fatema hasan
fatema hasan le 13 Déc 2020
  1. If A= <1,2,3>, B = <4,5,6>, write the complete MATLAB commands to compute the projection of A on B

fatema hasan
fatema hasan le 13 Déc 2020
  1. Compute the partial derivative of x2y3sin(e2xy) in MATLAB with respect to y
Answer:
Syms x,y,z
f=x^2 + y^3 + sin(x*y);
diff(f,x)
diff(f,y)

fatema hasan
fatema hasan le 13 Déc 2020
  1. Given two vectors: <1,4,3> and <1,0, -2>
Write the complete MATLAB commands to find the dot product

Brandon O'Neill
Brandon O'Neill le 26 Mar 2021
At a certain time of day the radiant energy from the sun reaches the roof along the direction given by the unit vector
The fraction of the sun’s energy which is falling perpendicularly on the roof is the projection of vector (A) onto the direction perpendicular to the roof – this is the dot product of (A) with the unit vector.
Q1) Use Matlab to calculate the fraction of the sun’s energy which is falling perpendicularly on the roof.
can anyone help with this?
the univ vector is [0.7627;0.5509;0.3390]
the vector A = 1/sqrt(21)[1;2;-4]

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by