How to convert a specific column to unit vector.
286 views (last 30 days)
Show older comments
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
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 ?
Answers (1)
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
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);
See Also
Categories
Find more on Logical 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!