Nested For Loops & Creating An Array

I had a question about how to successfully get this piece of code working properly. I start with an initial s value. The basic formula for calculating s is: s(k+1)=s(k)-u(k)+w(k). u may be any value between 0 and 20 in increments of 5 for any k. w is defined for each k as in the array I created below. What I need to end up with is an array of 5 elements in a row with a different value for each different u value used per w at that k and 9 columns (for k between 0 and 8 or k=1:9 in MATLAB terms). The initial value of s in each case will always be 70.
I'm thinking I can simply make a 1xn matrix and reshape it at the end, but the problem for me now is actually in my for loops. I need to generate the values for each s as I've described above before I can do that though. Obviously what I have now is probably incorrect although it may be close. I've tried my best and am simply stuck.
Any help would be much appreciated!
% initial s
s(1)=70;
w=[5 20 -5 5 5 15 -5 5];
u=[0 5 10 15 20];
% constraints
smin=0;
smax=140;
for k=1:8
for i=1:4
s(i+1)=s(i)-u(i+1)+w(k);
% indicate whether s is inplausible
if s(i+1)<smin || s(i)>smax
s(i+1)=-1;
end
end
end

回答(3 个)

Sounds sort of like,
cumsum( [repmat(70,size(u),1), bsxfun(@plus, -u.', w) ], 2)
except for that bit about setting s(i+1) to -1 in some cases, which would affect the sum for the rest of the row. Is it really intended to affect the rest of the row ??
Joseph Chrzan
Joseph Chrzan 2011-2-21

0 个投票

Yes, that part should definitely be outside of that loop and it should be using k rather than i.
One thing I forgot to add is that since there are 5 different u values for the first value of s (using the first value of w for all 5 calculations of s since k=1) then there will be 5 different values of s for the next iteration. Since there are still 5 different values of u then the amount of calculations will be squared. Basically this should calculate 5^k different values of s.

1 个评论

Why don't you post the corrected code and give the output you want to see so we can understand better. Give the s you think you should get.

请先登录,再进行评论。

Joseph Chrzan
Joseph Chrzan 2011-2-22

0 个投票

Okay, I'll try to describe what I'm trying to do a bit better:
k denotes a time step and there are 9 of them, 1 to 9. u denotes a choice that can be made for each time step. There are five different options to choose from every time and they do not change with s or k. s depends on the previous value of s as well as u and w via s(k+1)=s(k)-u+w(k). w has a static value for each different value of k (or for each time step) so there are nine values. There is also an initial value of s. So for the first iteration I would take the initial s, add the given value of w for the first time step k, and subtract the five different values of u, which will end up giving me five different possibilities for the next iteration for s. For each new s in the second iteration I will have a different value of w that doesn't change and the same five choices for u. However, because I have five different values of s from the first iteration, I now have 25 different possibilities for the next iteration of s. This goes on through all of the time steps, so as you can see, I'd need to end up with 5^k or 5^9 different possibilities for the final iteration.

2 个评论

Um, is this a Markov chain?
The final s possibilities depend on all of the previous s values so I don't think this is a Markov chain.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by