MATLAB Answers

Index exceeds the number of array elements (1)

6 ビュー (過去 30 日間)
Jonas Freiheit
Jonas Freiheit 2020 年 9 月 9 日
コメント済み: Jonas Freiheit 2020 年 9 月 10 日
%When I try to perform a row interchange for 'b' vector It tells me it exceeds the no of array elements. This is an attempt to perform partial pivoting for matrix A and vector b separately.
Thanks
A = [1 2 3;4 2 -1; 3 2 3];
b = [1; 3; 2];
k = 1;
A = pivoting(A,k)
function [A, b, flag] = pivoting(A, b, k)
n = size(A,1);
flag = 0;
for k=1:n-1
[big, i]=max(abs(A(k:n,k)))
ipr=i+k-1
if ipr~=k
A([k,ipr],:)=A([ipr,k],:)
b([k,ipr])=b([ipr,k])
end
end
end

  0 件のコメント

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

採用された回答

Kojiro Saito
Kojiro Saito 2020 年 9 月 9 日
It's because you're trying to manipulate b in line,
b([k,ipr])=b([ipr,k])
but, you're inputting as follows.
k = 1;
A = pivoting(A,k)
so, in function pivoting, b (second input variable) is scalar (1).
You just need to input three variables.
% A = pivoting(A,k)
A = pivoting(A,b,k)

  9 件のコメント

表示 6 件の古いコメント
Jonas Freiheit
Jonas Freiheit 2020 年 9 月 10 日
Yeah, including this code with only changing from the bold text code.
A = [1 2 3;4 2 -1; 3 2 3];
b = [1; 3; 2];
k = 1;
A = pivoting(A,k)
function [A, b, flag] = pivoting(A, b, k)
n = size(A,1);
flag = 0;
for k=1:n-1 %Here
[big, i]=max(abs(A(k:n,k)))
ipr=i+k-1
if ipr~=k
A([k,ipr],:)=A([ipr,k],:)
b([k,ipr])=b([ipr,k])
end
end
end %End
Thanks, Kojiro
Kojiro Saito
Kojiro Saito 2020 年 9 月 10 日
It's not possible only changing the bold text code because the funtion pivoting requires three inputs, so you need to modify input variables so that it allows two inputs.
Jonas Freiheit
Jonas Freiheit 2020 年 9 月 10 日
Alright that makes sense,
Thanks for the help Kojiro!

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by