Effacer les filtres
Effacer les filtres

How to check for and swap zero columns of a matrix to the end

2 vues (au cours des 30 derniers jours)
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

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Fév 2019
col = find( all(YourMatrix == 0) );
YourMatrix(:, [col end]) = YourMatrix(:,[end col]);
  2 commentaires
Faranak Sharifi-Babaki
Faranak Sharifi-Babaki le 1 Fév 2019
Modifié(e) : Faranak Sharifi-Babaki le 1 Fév 2019
Thank you so much! I really appreciate your help!
Also, I wrote this to swap rows in my solution vector:
b = R(:,4);
b([col r], :) = b([r col],:);
However it does not seem to work when there are multiple col values.
how would I swap multiple rows based on multiple col values on my solution vector? Thanks so much
Walter Roberson
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by