How to check for and swap zero columns of a matrix to the end
2 views (last 30 days)
Show older comments
Faranak Sharifi-Babaki
on 1 Feb 2019
Commented: Walter Roberson
on 1 Feb 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 Comments
Accepted Answer
Walter Roberson
on 1 Feb 2019
col = find( all(YourMatrix == 0) );
YourMatrix(:, [col end]) = YourMatrix(:,[end col]);
2 Comments
Walter Roberson
on 1 Feb 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.
More Answers (0)
See Also
Categories
Find more on Linear Least Squares 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!