extracting nonpivot columns from a matrix

조회 수: 12 (최근 30일)
Seungryul Lee
Seungryul Lee 2022년 10월 1일
답변: dpb 2022년 10월 1일
Hi, I have a function nonpuvotcols that takes a matrix as input and produces a matrix consisting of the nonpivot columns. For the last step of my code, I need to extract all the nonpivot colums from the input matrix, but I think I'm only extracting the last nonpivot column from the matrix.
% Returns the nonpivot columns from the matrix A
function npc = nonpivotcols(A)
% determine n the number of columns of A
[m, n_plus_one] = size(A)
n = n_plus_one-1
% use rref to obtain a list of the pivot columns of A (pcols)
[R pcols]=rref(A);
A(:,pcols)=[];
% from the list of all columns 1:n remove pcols to obtain a list of the nonpivot columns (fcols)
%% Getting columns which if fcol
[r,fcols]=size(A);
%(HELP) extract the nonpivot columns from A
npc = A(:,fcols);
end
This is my code. Please help me for the last commented part, which is to extract all the nonpivot columns.
A = [ 1 1 2 2 3 3; 4 4 5 5 6 6; 7 7 8 8 9 9]
[R, pcols] = rref(A)
nonpivotcols(A)
And here is the code to call the function.
Thank you!

채택된 답변

dpb
dpb 2022년 10월 1일
function npc = nonpivotcols(A)
[~,p]=rref(A);
A(:,pcols)=[];
npc=A;
end
or
function npc = nonpivotcols(A)
[~,p]=rref(A);
npc=A(:,setdiff(1:size(A,2),p));
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by