How should I alter this sum?

10 views (last 30 days)
James van Viersen
James van Viersen on 13 Nov 2012
Hey everybody, I would really appreciate help with the following:
for j=1:30
G=-N*mat(1,20-j)+N*mat(1,25-j)+N*(1/2)*mat(1,30-j)
end
Does somebody know how I can make sure the first two summands are only counted if 20-j=>0 and/or 25-j=>0? So for example if j=27 only the third summand is counted?
Thank you very much in advance!
James
[EDITED, copied from Answers section, Jan] Thank you very much already guys. What I actually meant was that for j up till 20 i would like
G=-N*mat(1,20-j)+N*mat(1,25-j)+N*(1/2)*mat(1,30-j)
then for 20<j<25 i would like
G=N*mat(1,25-j)+N*(1/2)*mat(1,30-j)
and for 25<j i would like
N*(1/2)*mat(1,30-j)
Also I posted a small simplified example, does anyone know a more concise and simpler way to implement this? I tried to work with something like -if- statements and min(0,20-j) but it didn't work out :)
thanks already very much for the help offered!
James
  3 Comments
Jan
Jan on 13 Nov 2012
[Copied from Comment to Comment to copied answer, Jan]
James van Viersen has written:
haha true sorry just looking for a solution with a -for- loop incorporated
Jan
Jan on 13 Nov 2012
Please, guys, stop posting comments to comments of comments in the section of comments to comments of answers, which are actually comments, which should appear as additions in the original question. Trying to clean this up makes me really nervous. ;-)

Sign in to comment.

Answers (3)

Matt J
Matt J on 13 Nov 2012
I assume you mean 20-j=>1 etc...
Z=zeros(1,30);
term1=Z;
term1(1:19)=-N*mat(1,19:-1:1);
term2=Z;
term2(1:24)=+N*mat(1,24:-1:1);
term3=Z;
term3(1:29)=+N/2*mat(1,29:-1:1);
G=term1+term2+term3;

José-Luis
José-Luis on 13 Nov 2012
Edited: José-Luis on 13 Nov 2012
Note the in mat(1,25-j) the index needs to be superior to 0, otherwise an error will be returned. Considering that:
nVals = 30;
term1 = zeros(1,nVals);
term2 = term1;
term3 = term1;
term1(end-20+1:end) = -N*mat(1,20:-1:1);
term2(end-25+1:end) = N*mat(1,25:-1:1);
term3(end-30+1:end) = (N/2)*mat(1,30:-1:1);
your_vals = term1 + term2 + term3;
That you could write as an anonymous function
term1 = zeros(1,nVals);
your_fill_fun = @(x,nFill) x(end-nFill+1:end) = mat(1,nFill:-1:1);
term1 = -N*your_fill_fun(term1,20); &etc

Jan
Jan on 13 Nov 2012
Edited: Jan on 13 Nov 2012
Your code example looks confusing, because the variable G is not used.
What about the trivial solution:
for j = 1:20
G = -N*mat(1,20-j)+N*mat(1,25-j)+N*(1/2)*mat(1,30-j);
end
for j = 21:24
G = N*mat(1,25-j)+N*(1/2)*mat(1,30-j);
end
for j = 26:30
G = N*(1/2)*mat(1,30-j);
end
This has three [EDITED from two] problems - I assume you have overseen them in your explanation:
  1. The first loop will try to access the index 20-j for j=20, but indicies must be greater than zero.
  2. The case j==25 is not considered.
  3. The case j==30 fails also due to zero indexing. [EDITED]
More compact soltutions:
for j = 1:30
if j < 20 % Or 21, please fix this by your own
G = -N*mat(1,20-j)+N*mat(1,25-j)+N*0.5*mat(1,30-j);
elseif j < 25
G = N*mat(1,25-j)+N*0.5*mat(1,30-j);
else
G = N*0.5*mat(1,30-j);
end
end
Or:
for j = 1:30
G = N*0.5*mat(1,30-j);
if j < 20 % Or 21, please fix this by your own
G = G - N*mat(1,20-j);
end
if j < 25
G = G + N*mat(1,25-j);
end
end
  1 Comment
James van Viersen
James van Viersen on 13 Nov 2012
wow thanks Jan I think I'll come a long way with all your input. Everybody thanks alot already!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!