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 visualización (últimos 30 días)
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!

Respuestas (1)

Walter Roberson
Walter Roberson el 29 de Sept. de 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 comentarios
Seungryul Lee
Seungryul Lee el 29 de Sept. de 2022
So do you mean I don't have any problem with the code?
I'm sure why I'm getting an error for this test.
Can you explain why?
Walter Roberson
Walter Roberson el 29 de Sept. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by