4 views (last 30 days)
Show older comments
Jonas Freiheit on 9 Sep 2020
Commented: Jonas Freiheit on 10 Sep 2020
Accepted Answer: Kojiro Saito
Open in MATLAB Online
%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 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Kojiro Saito on 9 Sep 2020
Open in MATLAB Online
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 Comments Show 7 older commentsHide 7 older comments
Show 7 older commentsHide 7 older comments
Jonas Freiheit on 9 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1002565
Edited: Jonas Freiheit on 9 Sep 2020
That worked for me but, Is there another way to solve this program successfully without changing the pivoting function and only the code in the for loop?
Thanks for the help Kojiro!
Kojiro Saito on 9 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1003276
Edited: Kojiro Saito on 9 Sep 2020
Open in MATLAB Online
It didn't mean changing pivoting function. You're calling pivoting with two inputs (A and k),
A = [1 2 3;4 2 -1; 3 2 3];
b = [1; 3; 2];
k = 1;
A = pivoting(A,k)
but I suggested that you would call pivoting with three inputs (A, b and k)
A = [1 2 3;4 2 -1; 3 2 3];
b = [1; 3; 2];
k = 1;
A = pivoting(A,b,k)
Jonas Freiheit on 10 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1003558
Sorry what I meant was is it possible to do it with only calling pivoting with two inputs (A and k)?
Kojiro Saito on 10 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1003591
Open in MATLAB Online
I see. Actually, k is initialized in pivoting function, so you don't need to input it.
The following would work.
A = [1 2 3;4 2 -1; 3 2 3];
b = [1; 3; 2];
%k = 1;
A = pivoting(A)
function [A, flag] = pivoting(A)
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
Jonas Freiheit on 10 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1003606
Edited: Jonas Freiheit on 10 Sep 2020
Sorry sorry, I mean only by changing this code:
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
Thanks, don't mean to annoy you, I'm trying to learn how to change the source code without changing the pivoting function.
Kojiro Saito on 10 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1003627
Open in MATLAB Online
The following is what you want to do?
A = [1 2 3;4 2 -1; 3 2 3];
b = [1; 3; 2];
%k = 1;
n = size(A,1);
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
Jonas Freiheit on 10 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1003639
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 on 10 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1003666
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 on 10 Sep 2020
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/590884-index-exceeds-the-number-of-array-elements-1#comment_1003687
Alright that makes sense,
Thanks for the help Kojiro!
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsMatrices and ArraysMatrix Indexing
Find more on Matrix Indexing in Help Center and File Exchange
Tags
- partial pivoting
- index
- elements
- array
- matlab
- error
- row interchange
- vector
- matrix
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office