creating a function nonpivotcols that takes a m*n matrix as input and produces a matrix consisting of the nonpivot columns of the input

3 次查看(过去 30 天)
Hi, I'm trying to create a function nonpivotcols that takes a m*n matrix as input and produces a matrix consisting of the nonpivot columns of the input matrix.
This is the code I have so far, but I'm not sure how to write a code for the (HELP) part..
Can't I just write it as the line %%Getting columns which it fcol ?
% 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)=[];
%(HELP) 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);
% extract the nonpivot columns from A
npc = A(:,fcols);
end
And this is the code to call the function:
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)
I'm getting an error for the third test code, with the variable size issue.
Please help!
Thank you!

回答(1 个)

Walter Roberson
Walter Roberson 2022-9-29
[R pcols]=rref(A);
A(:,pcols)=[];
You deleted the pivot columns out of A. Whatever is left must be non-pivot columns.
[r,fcols]=size(A);
fcols will be the number of columns in the portion of A that remains -- at this point, the number of non-pivot columns. fcols will be a scalar such as 6 if A were 4 x 6 at that point
npc = A(:,fcols);
A indexed with a scalar column index is going to return something that has exactly one column (provided that A is not empty... you would get an error if A were empty.)
But surely you want all of what remains of A, not just the last non-pivot column.

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by