creating a function nonpivotcols that takes a m*n matrix as input and produces a matrix consisting of the nonpivot columns of the input
1 view (last 30 days)
Show older comments
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140560/image.jpeg)
Please help!
Thank you!
0 Comments
Answers (1)
Walter Roberson
on 29 Sep 2022
[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.
2 Comments
Walter Roberson
on 29 Sep 2022
No, I am pointing out that your code is always extracting the last non-pivot column, when your task is to extract all of the non-pivot columns.
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!