How do i fix the error that i am getting?

My code is
A = [1,1,0,2,0;0,1,1,3,0;2,0,0,0,1;3,1,0,2,1;2,1,1,3,0;1,0,0,2,1]
rrefA = rref(A)
basisRowSpace = A(rrefA(:,end)~=0,:)
pivotColumns = rrefA(:,1:end-1)
basisColumnSpace = A(:,pivotColumns)
This is where it stops working and I dont know how to fix it.
The error that I get is Index in postion 2 is invails. Array indices must be postive integers or logical values.
I would appricate any help to be able to fix this and get it to run.
Thank You.

 Accepted Answer

Torsten
Torsten on 13 Jul 2023
Moved: Torsten on 13 Jul 2023
Remove the semicolon behind the line
pivotColumns = rrefA(:,1:end-1);
execute the code and see if it makes sense to insert the displayed matrix in
basisColumnSpace = A(:,pivotColumns);

7 Comments

This still does not seem to fix the code. And even if I try putting just the matrix into the basisColumnSpace code it still gives me the same error. Thanks for try though.
Though I may not be understanding what you are telling me to do.
"pivotColumns" must be a vector of length <= 5 with entries between 1 and 5 that identifies the linear independent columns of the matrix A.
What you insert for "pivotColumns" is a matrix consisting of 0's and 1's.
This might be of interest:
A = [1,1,0,2,0;0,1,1,3,0;2,0,0,0,1;3,1,0,2,1;2,1,1,3,0;1,0,0,2,1];
licols(A)
ans = 6×5
1 1 0 2 0 0 1 1 3 0 2 0 0 0 1 3 1 0 2 1 2 1 1 3 0 1 0 0 2 1
function [Xsub,idx]=licols(X,tol)
%Extract a linearly independent set of columns of a given matrix X
%
% [Xsub,idx]=licols(X)
%
%in:
%
% X: The given input matrix
% tol: A rank estimation tolerance. Default=1e-10
%
%out:
%
% Xsub: The extracted columns of X
% idx: The indices (into X) of the extracted columns
if ~nnz(X) %X has no non-zeros and hence no independent columns
Xsub=[]; idx=[];
return
end
if nargin<2, tol=1e-10; end
[Q, R, E] = qr(X,0);
if ~isvector(R)
diagr = abs(diag(R));
else
diagr = abs(R(1));
end
%Rank estimation
r = find(diagr >= tol*diagr(1), 1, 'last'); %rank estimation
idx=sort(E(1:r));
Xsub=X(:,idx);
end
Thus all 5 columns of A are linearly independent.
That is what i tried to insert the answer to pivotColumns that I get is [1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;0,0,0,0;0,0,0,0]and when i tried inserting that into the basiscolumnSpace it stil didn't work.
What is your code supposed to do ? Extract the maximum number of linearly independent columns of the matrix A ? Then use "licols" from above.
I am supposed to be finding the basis for column space.
Ok, that's what "licols" does. The maximum number of independent columns of A is a basis for the column space of A.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 13 Jul 2023

Commented:

on 13 Jul 2023

Community Treasure Hunt

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

Start Hunting!