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.

回答(3 个)

Walter Roberson
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 个评论
Atr cheema
Atr cheema 2015-11-24
Suppose t=1:10 and g=21:30. I want formula to run first for t=1 and g=21 then the formula should run for t=2 and g = 21,22. Then the formula should use t=3 and g=21,22,23 and so on and so forth till it takes t = 10 and g=21:30 (now all value of g). Can you please elaborate it a bit.
Walter Roberson
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
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.

Image Analyst
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 个评论
Atr cheema
Atr cheema 2015-11-24
Here the problem is that it will use all values of vector g for one value of t. But I want that the formula uses only one value of vector g for one value of vector t. Then it should go to next value of t and next value of g.
Image Analyst
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 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