How to check for and swap zero columns of a matrix to the end
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2019-2-1
col = find( all(YourMatrix == 0) );
YourMatrix(:, [col end]) = YourMatrix(:,[end col]);
2 个评论
Walter Roberson
2019-2-1
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!