Nested for loop outputs

1 次查看(过去 30 天)
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

采纳的回答

Daniel kiracofe
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 个评论
Brittany Caughron
Brittany Caughron 2016-11-4
Sorry for being vague. I intentionally left out a lot of details because i figured there was an easy way to do this and sometimes less is more. Anyways, this resolved my issue, so thank you!

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
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.
See the FAQ for a good explanation of cell arrays: The FAQ entry on cell arrays
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.
  1 个评论
Brittany Caughron
Brittany Caughron 2016-11-4
Thanks for your reply. T is just a scalar. Setting Tvalue(i) = some_other_matrix(end) got me where i needed to be.
Sorry the problem wasn't clear. I didnt think I'd need to post many details for what I assumed was an easy fix. As it turns out, you both were confused by the question but still managed to point me in the right direction so thanks!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by