Nested for loop outputs
1 回表示 (過去 30 日間)
古いコメントを表示
Brittany Caughron
2016 年 11 月 4 日
コメント済み: Brittany Caughron
2016 年 11 月 4 日
Hopefully a simple question:
I've got a program with a for loop inside another for loop that looks something like this:
x=0:some value
for i = 100 iterations
T = defined equation
Tvalue(i) = T %this stores all 100 results of T in a matrix
if [condition logic]
% if true, repeat for next i
else
for j = 1000 iterations
T = %redefined equation
Tvalue(j) = T % different size matrix
end
end
plot(x,Tvalue)
Right now, my program is essentially working, but I want to take the last value of T(j) and input it as that iteration of i's value in the T(i) matrix and keep going until i = 100th iteration. In other words, (assuming condition logic is false) for i = 2, i want the 1000th iteration of T(j) to become the 2nd value of T(i) in the first matrix.
Right now the program appears to be having trouble with the matrices being different sizes.
Thank you for any guidance
0 件のコメント
採用された回答
Daniel kiracofe
2016 年 11 月 4 日
I'm not sure if entirely understand the question... why not just do this for the last block?
for j = 1000 iterations
T = %redefined equation
some_other_matrix(j) = T
end
Tvalue(i) = some_other_matrix(1000)
If that doesn't answer the question, then I'd suggest you post more information. For example " the program appears to be having trouble" is pretty vague. If you are getting an error message, we'd need to see your entire code, and the exact error message.
その他の回答 (1 件)
Image Analyst
2016 年 11 月 4 日
If T is a matrix, you can't do this:
Tvalue(j) = T
because in the case where you're assigning something to an element of a vector, that something must be a scalar.
T can be a matrix, even of different sizes on different iterations, if Tvalue is a cell array instead of a normal regular double vector. In that case, you'd do
Tvalue{j} = T % Use braces instead of parentheses.
I really don't know what you mean when you say "want to take the last value of T(j) and input it as that iteration of i's value in the T(i) matrix". Well, whatever the last value of T(j) is, we don't know how this can go into the definition of T(i) because you just say
T = defined equation
but we have no idea what that equation is or if it involves T(j). Why don't you just set T(i) = T(end)?
T = T(end);
Actually we don't even know from your pseudocode if T is even a vector at all. From the looks of it, T is just a scalar, like I said it must be if you're going to assign it to a single element of Tvalue. The whole problem description is a murky mess to me.
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!