How to check for and swap zero columns of a matrix to the end
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Faranak Sharifi-Babaki
le 1 Fév 2019
Commenté : Walter Roberson
le 1 Fév 2019
Hi,
I am working on an assignment where I will have a function that takes in 3 row vectors (eg. plane1, plane2, plane3) in form of [a1, a2, a3, c]. It is then supposed to make a matrix with the vectors and perform rref to find the weight vector (Pspace) and the null space (Nspace):
function [Pspace, Nspace] = a1 (plane1, plane2, plane3).
However, if the matrix ends up having a zero column, the weight vector (Pspace) given is incorrect. My question is how do I check for a zero column vector and if there is a predefined function for that in matlab. Also, how will I swap the zero column vector to be the last vector in the matrix?
Here is an example of what I mean:
If I was given the matrix:
-1 0 -3 and I was supposed to find a weight vector so it equaled: 24
-2 0 7 -69
4 0 8 -60
How can I check that there is the zero column vector and how can I swap column 2 with column 3 so it looks like this:
-1 -3 0 Also how can I swap row2 with row3 of my solution vector: 24
-2 7 0 -60
4 8 0 -69
0 commentaires
Réponse acceptée
Walter Roberson
le 1 Fév 2019
col = find( all(YourMatrix == 0) );
YourMatrix(:, [col end]) = YourMatrix(:,[end col]);
2 commentaires
Walter Roberson
le 1 Fév 2019
col = find( all(YourMatrix == 0) );
nc = length(col);
YourMatrix(:, [col, end-nc+1:end]) = YourMatrix(:,[end-nc+1:end, col]);
Note that this is likely to run into problems if one of the last columns is already 0. This is because you defined the operation as swapping with the end rather than swapping it with a non-zero column.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Linear Least Squares 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!