extracting nonpivot columns from a matrix

10 次查看(过去 30 天)
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 个)

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by