Iteration of changing discrete variables

9 Ansichten (letzte 30 Tage)
Claire
Claire am 2 Feb. 2011
Kommentiert: Steven Lord am 27 Aug. 2021
I'm not sure that's the best way to describe what I want to do and I'm sure it's really basic, but I'm stuck. I've got 4 variables, a, b, c, d and a series of equations they are used in. I also have several years worth of data for these four variables, but I don't know how to write it so that I can make the four variables change at the same time. So as an example, loop one would be: a=10, b=30, c=25, d= 40, then my next one would be a=25, b=32, c=20, d =50, etc.
All I've come across are loops that write it for variables that have a standard increase.
Thank you.

Antworten (3)

Martijn
Martijn am 2 Feb. 2011
You do not necessarily need to have a "standard increase" in a FOR-loop variable, for example the following code is valid:
for a=[2 3 5 7 11 13 17 19]
isprime(a)
end
However, I do not think that is what you are looking for as you have multiple variables changing at the same time. A possible solution then could be to first define the values which you want to use in a matrix:
A = [10 30 25 40;25 32 20 50];
And then use:
for i=1:size(A,1)
a = A(i,1);
b = A(i,2);
c = A(i,3);
d = A(i,4);
% Do something with a, b, c and d
end

David Young
David Young am 2 Feb. 2011
It depends on how the data for the four variables are stored. I'll assume you can read the data into four arrays, each of which is a single row, called Adata etc. If you don't know how to put your data into this form, do ask, giving more details about how it is stored.
Then, to answer your question, the code to take successive values would go something like this:
for k = 1:length(Adata) % assume all data arrays same length
a = Adata(k);
b = Bdata(k);
c = Cdata(k);
d = Ddata(k);
% ... do stuff with a, b, c and d here
end % end of the for loop
Something very similar would work in most programming languages. However, in Matlab you often don't need to write loops like this at all, and you can operate on all the elements of Adata, Bdata, Cdata and Ddata together, using vectorised code. If you can do this, it's a more effective way to use Matlab - but your priority is probably to get something working first.
  1 Kommentar
Claire
Claire am 2 Feb. 2011
Thank you very much for your help.
I've got all of my results output in a table, but what I really need to do now, is link them to a day and hour time- they're hourly results and I've no idea how to do it. Can I get them to output in the same table/ display together? I hope that makes some sense.
Also what do you mean by vectorised code? I've had problems with it saying I don't have enough memory to perform the programme, would a vectorised code help?
Thank you.

Melden Sie sich an, um zu kommentieren.


noor almasry
noor almasry am 27 Aug. 2021
How many times will the display command in the following script be executed?
x = 2.5;
while (x < 19) disp ('Am I done yet?')
x = x + 2.5 ;
end
  1 Kommentar
Steven Lord
Steven Lord am 27 Aug. 2021
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial (https://www.mathworks.com/support/learn-with-matlab-tutorials.html) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with MATLAB 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