For loop inside structure
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mohammed Hammad
am 24 Jan. 2019
Bearbeitet: Mohammed Hammad
am 26 Jan. 2019
I have a structure called (level3) and inside it some fields (e.g x0, vx). each field is an array with some values as shown in the screenshot.
I would like to go through these fields values and multply them as follows:
x(1,1)*vx(1,1)
x(2,1)*vx(2,1)
x(3,1)*vx(3,1)
.... and so on.
so I got at the end 10 arrays of results of x
I tried to do it like this but I just got one array with the last value.
fields = fieldnames(level3);
for k=1:10
x = [x0. * vx];
end
Thanks in advance
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/201435/image.png)
0 Kommentare
Akzeptierte Antwort
Mohammed Hammad
am 25 Jan. 2019
Bearbeitet: Mohammed Hammad
am 26 Jan. 2019
2 Kommentare
Stephen23
am 26 Jan. 2019
Bearbeitet: Stephen23
am 26 Jan. 2019
Those square brackets are superfluous. Get rid of them.
Note that your inner loop serves no purpose, because you do not use its loop index anywhere and none of its iterations depend on previous iterations, so the results of every of its iteration will get discarded except for the last one.
Weitere Antworten (1)
Bob Thompson
am 24 Jan. 2019
fields = fieldnames(level3);
for k=1:10
x = [x0. * vx];
end
If I understand this correctly you're trying to capture the array of multiplications (x0 .* vx) for all of the different elements of the structure. I believe that all you're missing then is either an index on x, or to concatonate the results of x with the previous values.
fields = fieldnames(level3)
x = [];
for k = 1:10
x = [x;[x0.*vx]];
end
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!