How to convert a specific column to unit vector.
35 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2015-12-1
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 ?
回答(1 个)
Walter Roberson
2015-12-1
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 个评论
Walter Roberson
2015-12-1
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);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!