필터 지우기
필터 지우기

Nested for loop outputs

조회 수: 2 (최근 30일)
Brittany Caughron
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

채택된 답변

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