Anyone can help me to improve this code?

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

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 5월 22일
편집: Andrei Bobrov 2013년 5월 22일

0 개 추천

[EDIT]
n = 5; %
R = [0 0 0 1 1]';% eg
c = 2; %
A1 = spdiags(-c*R,1,n,n);
A = A1 + A1' + spdiags(c*(R + [R(2:end);0]),0,n,n);
full(A)
OR
Rin(:,[1 3 2]) = c*[-[circshift(R,-1) R],R + [R(2:end);0]];
A = spdiags(Rin,-1:1,n,n);
full(A)
OR
Rin(:,[1 3 2]) = c*[-[circshift(R,-1) R],conv(R,[1;1],'same')];
A = spdiags(Rin,-1:1,n,n);
full(A)

댓글 수: 3

Brwa
Brwa 2013년 5월 22일
편집: Brwa 2013년 5월 23일
Thank you very much you you done very well. I really appreciate your help and effort. and thanks to David Sanchez too.
wish you all the best guys
Andrei Bobrov
Andrei Bobrov 2013년 5월 22일
EDITed
Brwa
Brwa 2013년 5월 22일
still have the same mistake, the diagonal line should not be sum of
A(1,1) must not equal A(5,5)+A(4,4)+A(3,3)+A(2,2)+c
if R =[ 1 1 1 1 1]
A(5,5) must equa = c
A(4,4) must equal A(5,5) + c
A(3,3) must equal A(4,4) + c
A(2,2) must equal A(3,3) + c
A(1,1) must equal A(2,2) + c
And if
if R =[ 1 1 1 0 1]
A(5,5) must equa = c
A(4,4) must equal A(5,5) + 0
A(3,3) must equal 0 + c this means we only consider about original number of A(4,4) when R =[ 1 1 1 0 1] the fourth value is 0 so A(4,4) = 0 therefore, A(3,3) = 0 + c = c
A(2,2) must equal A(3,3) + c
A(1,1) must equal A(2,2) + c

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

David Sanchez
David Sanchez 2013년 5월 22일

0 개 추천

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

Brwa
Brwa 2013년 5월 22일
Dear Aavid Sanchez thanks for your help but your code is also not working
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?
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
Brwa 2013년 5월 22일
oh, my god still not correct, but I still appreciate your help very very much.
the problem is for R= [ 1 1 1 0 1]
A(3,3) must = c not 2c because A(4,4) =0 so A(3,3) = c + 0 = c

댓글을 달려면 로그인하십시오.

Brwa
Brwa 2013년 5월 22일

0 개 추천

Thanks for your helps, Andrei Bobrov and David Sanchez. you guys are so nice. This is my code. it works good, to be honest i learnt alot from you two. but if someone can ake it shorter that will be great.
n=5;
A = zeros(n,n);
R = [1;1;0;1;1];
c = 2;
for i = n-1 : -1 : 2;
if R(i,1) == 1 && R(i+1,1) ==1
A(i,i) = 2*c ;
A(i,i-1) = - c ;
A(i-1,i) = - c ;
elseif R(i,1) == 1 && R(i+1,1) ~=1
A(i,i) = c ;
A(i,i-1) = - c ;
A(i-1,i) = - c ;
elseif R(i,1) ~= 1 && R(i+1,1) ==1
A(i,i) = c ;
A(i,i-1) = 0 ;
A(i-1,i) = 0 ;
elseif R(i,1) ~= 1
A(i,i) = 0 ;
A(i,i-1) = 0 ;
A(i-1,i) = 0 ;
end
if R(1,1)== 1 && R(2,1) == 1
A(1,1) = 2*c ;
elseif R(1,1) == 1 || R(2,1) == 1
A(1,1) = c;
else
A(1,1) = 0;
end
if R(n,1) ==1 && R(n-1,1) ==1
A(n,n) = c ;
A(n-1,n) = -c;
A(n,n-1) = -c;
A(n-1,n-1) = 2*c;
elseif R(n,1) ==1
A(n,n) = c ;
A(n-1,n) = -c;
A(n,n-1) = -c;
A(n-1,n-1) = c;
end
end
Thank you again

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2013년 5월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by