フィルターのクリア

Writing a Gaussion Elimination function with 4 inputs

3 ビュー (過去 30 日間)
Vasavi_Shakti
Vasavi_Shakti 2018 年 2 月 7 日
回答済み: Aveek Podder 2018 年 2 月 20 日
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 件のコメント
John D'Errico
John D'Errico 2018 年 2 月 7 日
What error do you get? Please report the entire text of the error.

サインインしてコメントする。

回答 (1 件)

Aveek Podder
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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by