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 (최근 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!
댓글 수: 0
답변 (1개)
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.
댓글 수: 2
Walter Roberson
2022년 9월 29일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!