Row Echeleon Form/Forward elimination only
조회 수: 9 (최근 30일)
이전 댓글 표시
I am having trouble coding something that will just give me the matrix in row echelon form. I put asterisks around the line that isnt working and the error given is "Subscripted assignment dimension mismatch". I am not sure how to fix it. Below is also the PDF with a test case.
function [A_new, b_new] = forward_elimination(A, b)
%FORWARD_ELIMINATION - Performs forward elimination to put A into unit
% upper triangular form.
% A - original matrix of Ax = b
% b - original vector of Ax = b
% A_new - unit upper triangular A formed using Gaussian Elimination
% b_new - the vector b associated with the transformed A
% Default output
% A_new = A;
% b_new = b;
%********************************** TODO ********************************
% Perform Gaussian Elimination to evaluate turn A into a unit upper
% triangular matrix
[rowA,colA]=size(A);
[rowb,colb]=size(b);
if det(A)==0
disp('Error')
A_new=zeros(rowA,colA);
b_new=zeros(rowb,colb);
elseif A==zeros(rowA,colA)
disp('Error')
A_new=zeros(rowA,colA);
b_new=zeros(rowb,colb);
else
sysarray=[A b];
row1=sysarray(1,:);
element1=A(1,1);
rrow1=(row1/element1);
array2=[rrow1;sysarray(2:end,:)];
for i=1:(numel(b)-1)
*array3(i)=array2(i+1,:)-array2(i,:).*array2(i+1,i+1);*
end
n_array2=array3;
for j=1:(rowA-1)
newArray=n_array2(j,:)/n_array2(j,j);
end
A_new=newArray(:,end-1);
b_new=newArray(:,end);
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168795/image.png)
댓글 수: 0
채택된 답변
Aveek Podder
2017년 11월 7일
Hi,
You are accessing the element of array3 by using linear indexing and you trying to fill it with a row vector. So you are getting a dimension mismatch error.
This issue can be solved by replacing the line within asterisks with the line given below.
array3(i,:) = array2(i+1,:) - array2(i,:).*array2(i+1,i+1);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!