MATLAB Answers


Writing a Gaussion Elimination function with 4 inputs

Asked by Vasavi_Shakti on 7 Feb 2018
Latest activity Answered by Aveek Podder 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');
for j = 1:m-1
A(rb,j) = A(rb,:) + k*A(rb:j+1);
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)

  1 Comment

What error do you get? Please report the entire text of the error.

Sign in to comment.

1 Answer

Answer by Aveek Podder on 20 Feb 2018

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:


Sign in to comment.