Writing a Gaussion Elimination function with 4 inputs

1 view (last 30 days)
Hello Matlab community,
As i'm new to Matlab, i'm trying to write a function that performs Gaussian Elimination on a matrix. The function is the MultAddRow fuction which requires 4 inputs: matrix A is the input. The constant k is multiplied to row 'ra' which is added to row 'rb'.
I've written the function, but i just can't calculate the eliminated matrix, as i get an error:
function A = MultAddRow(A,k,ra,rb)
[m,n] = size(A);
if ra < 1 || ra > m || rb < 1 || rb > m
error('ra or rb are not available rows in matrix A');
end
for j = 1:m-1
A(rb,j) = A(rb,:) + k*A(rb:j+1);
end
I've tried calling the function this way, in a different script:
A=[1 2 3 0; -3 7 -1 2; -2 0 5 4];
A = MultAddRow(A,3,1,2)
disp(A)

Answers (1)

Aveek Podder
Aveek Podder on 20 Feb 2018
Hi,
I assume that you are receiving the following error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in MultAddRow (line 7)
A(rb,j) = A(rb,:) + k*A(rb:j+1);
The reason for this error is due to the dimension mismatch of A(rb,:) and A(rb:j+1).
While accessing the elements of A by A(rb:j+1), you are performing linear indexing. Thus A(rb:j+1) returns a vector of different dimension as A(rb,:).
For more information on Linear Indexing of a Matrix please have a look at the following link: https://www.mathworks.com/help/matlab/math/matrix-indexing.html

Community Treasure Hunt

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

Start Hunting!