Writing a Gaussion Elimination function with 4 inputs

Vasavi_Shakti (view profile)

on 7 Feb 2018
Latest activity Answered by Aveek Podder

Aveek Podder (view profile)

on 20 Feb 2018
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];
disp(A)

John D'Errico

John D'Errico (view profile)

on 7 Feb 2018
What error do you get? Please report the entire text of the error.

Answer by Aveek Podder

Aveek Podder (view profile)

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