How to convert a specific column to unit vector.
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 1 Déc 2015
1 2 3 4
5 6 7 8
9 10 11 12
has 6 only in 1 column.
Are you trying to produce the vector [2;6;10] or the vector [5 6 7 8]? Or something else?
Yuvaraj Pazhamalai
le 1 Déc 2015
Walter Roberson
le 1 Déc 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
le 1 Déc 2015
Réponses (1)
Walter Roberson
le 1 Déc 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 commentaires
Yuvaraj Pazhamalai
le 1 Déc 2015
Walter Roberson
le 1 Déc 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);
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!