Writing a Gaussion Elimination function with 4 inputs
1 次查看(过去 30 天)
显示 更早的评论
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)
1 个评论
回答(1 个)
Aveek Podder
2018-2-20
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!