Anyone can help me to improve this code?
Show older comments
Dear friends; I have a code which is not suitable for big matrices or unlimited matrice. I would like to make it better because some times i have a very big matrices such as A = (100,100)
A = zeros(n,n) matrix.
R = [ 0 0 0 1 1] vector
c=2
if R(5,1) == 1
A(5,5) = c
A(5,4) = - c
A(4,5) = - c
else
A(5,5) = 0
A(5,4) = 0
A(4,5) = 0
end
if R(4,1) == 1
A(4,4) = c + A(5,5)
A(3,4) = - c
A(4,3) = - c
else
A(4,4) = 0
A(3,4) = 0
A(4,3) = 0
end
.
.
.
ans
A =
0 0 0 0 0
0 0 0 0 0
0 0 0 -2 0
0 0 -2 4 -2
0 0 0 -2 2
Thanks in advance, your help always appreciated
Accepted Answer
More Answers (2)
David Sanchez
on 22 May 2013
Ii this what you need?
for k=1:n-1
if R(k,1) == 1
A(k,k)= c + A(k+1,k+1);
else
A(k,k) = A(k+1,k+1);
end
4 Comments
Brwa
on 22 May 2013
David Sanchez
on 22 May 2013
for k=1:(n-1)
if R(k) == 1
A(k,k)= c + A(k+1,k+1);
else
A(k,k) = A(k+1,k+1);
end
Do you get an error message or aren't you getting what you want?
David Sanchez
on 22 May 2013
I see, you have to start the for loop by the end.
c=2;
if R(5,1) == 1
A(5,5) = c
A(5,4) = - c
A(4,5) = - c
else
A(5,5) = 0
A(5,4) = 0
A(4,5) = 0
end
for k=(n-1):-1:1
if R(k) == 1
A(k,k)= c + A(k+1,k+1);
else
A(k,k) = A(k+1,k+1);
end
Brwa
on 22 May 2013
Brwa
on 22 May 2013
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!