필터 지우기
필터 지우기

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

채택된 답변

Walter Roberson
Walter Roberson 2019년 2월 1일
col = find( all(YourMatrix == 0) );
YourMatrix(:, [col end]) = YourMatrix(:,[end col]);
  댓글 수: 2
Faranak Sharifi-Babaki
Faranak Sharifi-Babaki 2019년 2월 1일
편집: Faranak Sharifi-Babaki 2019년 2월 1일
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 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 CenterFile Exchange에서 Linear Least Squares에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by