Using structures in functions within for loop
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I've been cleaning up the workspace and editor a little by placing variables within structures. But I'm struggling with accessing and indexing structures and fields within structures in loops and functions within these loops.
Say I bundled up all key variables (1x1 doubles) into a structure, 'S' and pass them into a function, 'myfunction' which unpacks these variables, does some simple calculations (returning some 1x1doubles) then returns a structure of results, 'R'. Say I then pick one of these variables, 'V' to iterate over to produce an array of results from the function. How do I enter the structures into the function and return them within a for loop?
For example:
S.A = 10
S.B = 100
S.V = 1:1:10;
for i = 1:length(S.V)
[R(i)] = myfuction(S(i));
end
function [R] = myfunction(S)
R.C = S.A * S.V;
R.D = S.B * S.V;
end
Obviously the example above is super simple but I'm just trying to get my head around using structures within functions within loops (or another way to bundle up variables). The for loop would work for variables but how do I make it work for structures (or another form of bundling many variables)?
Kind regards,
Oliver
0 comentarios
Respuestas (1)
Dave B
el 10 de Nov. de 2021
In your example, you have a bunch of values for V (i.e. a 1 x 10 vector) and single values (i.e. scalars) for A and B. Do you want the function to use the the scalars for A and B and each of the values for V?
Several options here. First of all, in this limited case you don't need a loop. You're simply returning a vector C and D with corresponding length to the vector V
S.A = 10;
S.B = 100;
S.V = 1:1:10;
R = myfunction(S);
function R = myfunction(S)
R.C = S.A * S.V;
R.D = S.B * S.V;
end
A second strategy would be to make an array S, each one containing the values of A, B, and V:
for i = 1:10
S(i).A = 10;
S(i).B = 100;
S(i).V = i;
end
for i = 1:numel(s)
R(i) = myfunction(S(i));
end
% same myfunction as above
A third strategy would be to pass in which value of V you wanted:
S.A = 10
S.B = 100
S.V = 1:1:10;
for i = 1:length(S.V)
R(i) = myfuction(S,i);
end
function [R] = myfunction(S)
R.C = S.A * S.V(i);
R.D = S.B * S.V(i);
end
Of course, there are infinitely more options depending on what the harder problems are.
A good way to think about this space maybe is to consider: a struct can be a scalar or an array; each of a struct's fields can each be a scalar or an array.
2 comentarios
Dave B
el 11 de Nov. de 2021
Oliver -
I think the key here is that you have some knowledge about how you want to sweep across parameters but you need to pass that information to MATLAB somewhere. The case you describe of having to sweep over two of the variables in particular makes me think there's some information that you have that you need to communicate to MATLAB? (i.e. when it should it do a(i)+b(i)+c vs. a(i)+b(j)+c if that makes sense)
WRT 1: I'm sure you've considered this, but it's really important to note that lots of things in MATLAB vectorizable (including for example raising to a power), and even your nested cases may be manageable if you think about them as a matrix. Of course there's lots of logic that can be conceived in an array sense, but I mention this because vectorized operations are core to writing idiomatic MATLAB code (and often how you make things speedy).
WRT 2: worth trying to see if it matters?
WRT 3: If you wanted to treat all vectors in the same way, in theory you could write this code to cycle over vectors and index them with i and take scalars as is. I don't recommend that, it sounds messy!
Ver también
Categorías
Más información sobre Matrix Indexing 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!