Iteration of changing discrete variables
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (3)
Martijn
el 2 de Feb. de 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
0 comentarios
David Young
el 2 de Feb. de 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.
noor almasry
el 27 de Ag. de 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 comentario
Steven Lord
el 27 de Ag. de 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.
Ver también
Categorías
Más información sobre Get Started with MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!