How can I change two variables in a MATLAB at same time?
16 次查看(过去 30 天)
显示 更早的评论
I have written a for loop in which variable t changes. I want another variable g to vary at same time as t varies. I want to give one different/specific value of g (which is calculated before by another for loop) for each value of t.
0 个评论
回答(3 个)
Walter Roberson
2015-11-24
Presuming that tvals is a vector of the values for t that you want, and that g is a vector of the corresponding g values, then
for tidx = 1 : length(tvals)
t = tvals(tidx);
this_g = g(tidx);
....
result(tidx) = whatever
end
2 个评论
Walter Roberson
2015-11-24
tvals = 1 : 10;
gvals = 21:30;
for tidx = 1 : length(tvals)
t = tvals(tidx);
g = gvals(1:tidx);
....
result(tidx) = whatever
end
Image Analyst
2015-11-24
Based on your comment to Walter, it looks like you want to define t and g in advance and then use one element of t, and a sub-vector of g, in the computation of your output/result. This should do it:
% Define t and g
t=1:10;
g=21:30
% Use a loop to compute a value that depends on t(k) and g(1:k)
for k = 1 : length(t)
% Get the scalar t, and g array needed
this_t = t(k);
this_g = g(1:k);
% Now pass those to some function
% that will compute an output value.
output(k) = SomeFunction(this_t, this_g);
end
% Print to command window
output
Note that you did not tell us the computation that should be done with t and the subvector of g so I just wrapped that up in a function called SomeFunction.
0 个评论
Image Analyst
2015-11-24
Not sure what you mean by "at the same time" but the only way I know of to make variables change value at the same time is to have their computation take place on different CPUs and this requires the Parallel Processing Toolbox.
But it sounds like you don't really need them to change at the same time. It sounds like you can just call the other "for loop" to compute the g before the value of t uses it to produce a new value of t. One example might be if t changes in a loop and g changes in another loop:
for kt = 1 : 42
for kg = 1 : 73
% some code that produces a final value for "g" when it's done.
end
t = ...some formula that depends on "g".....
end
That's the only guess I'm going to make. If you want more specific advice, you should read this and supply more specific information.
2 个评论
Image Analyst
2015-11-24
No. I didn't say that t had to use all values of g. You could use just element #42 in your calculation for t if you want. You just said that you wanted to give a value of g to the computation for t but you didn't say which element of g you wanted to give to t. Do you want to use the kth element of g in the computation of the kth element of t??? If so, you don't even need an array for g - it can simply be a scalar, unless you need it later for some reason. You didn't say that you need to keep g hanging around after t is computer, so let's just make g a scalar.
for k = 1 : whatever
g = some scalar from some computation
t(k) = some formula involving scalar g
end
Let me read your other comments where you added additional explanation/clarification.
另请参阅
类别
在 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!