Nested for loop outputs

1 Ansicht (letzte 30 Tage)
Brittany Caughron
Brittany Caughron am 4 Nov. 2016
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

Akzeptierte Antwort

Daniel kiracofe
Daniel kiracofe am 4 Nov. 2016
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 Kommentar
Brittany Caughron
Brittany Caughron am 4 Nov. 2016
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 4 Nov. 2016
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 Kommentar
Brittany Caughron
Brittany Caughron am 4 Nov. 2016
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by