How to convert a specific column to unit vector.

286 views (last 30 days)
Let's say i have the following matrix [1 2 3 4; 5 6 7 8; 9 10 11 12]
For a certain condition, "6" satisfies the condition. Now i want to make the 2 column in which 6 is present a unit vector. I am able to do it for one iteration by manually coding the row operations but if the next element that satifies the condition happens to be in (3,1).
How to create a macro of sorts to solve this in MATLAB. Appreciate the help. Thanks
  4 Comments
Walter Roberson
Walter Roberson on 1 Dec 2015
Do you mean like [2;6;10] / norm([2;6;10]) ? "unit vector" in the sense that the sum of the squares of its components is 1.0 ?
Yuvaraj Pazhamalai
Yuvaraj Pazhamalai on 1 Dec 2015
No, not really. i want row operations to be done on the entire matrix so that the 2nd column become [0 1 0] and the other columns will change corresponding to the row operations.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 1 Dec 2015
If you have a vector V that you want to convert to a "unit vector" in the sense that the magnitude of the unit vector is 1.0, then
unit_V = V/.norm(V);
  2 Comments
Yuvaraj Pazhamalai
Yuvaraj Pazhamalai on 1 Dec 2015
Sorry, this is not what i wanted. I want the row operations like the following: R2=R2/6 R1 = R1-2*(R2/6) etc.
Walter Roberson
Walter Roberson on 1 Dec 2015
In order to hope to be able to do that, you would have to write a MATLAB class that implemented all of the operations. I am not certain that it can be done at all: the one operation that cannot be overridden in MATLAB is assignment, so R2 = R2/6 would not be able to associate the destination "R2" with the original matrix.
It would be easier if you could accept a syntax such as
M.R2 = M.R2/6;
M.R1 = M.R1 - 2*(M.R2/6);
then this could be coded as getting or setting properties of the object M with the object M retaining its type.
Setting all of this up would certainly take more effort than just using normal matrix indexing,
M(2,:) = M(2,:)/6;
M(1,:) = M(1,:) - 2*(M(2,:)/6);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!