MATLAB Answers

is there any way to set up a script to run a general amount of nested loops?

1 view (last 30 days)
Thomas McLean
Thomas McLean on 16 Mar 2018
Edited: Adam on 16 Mar 2018
for example if i have a 3d array is there anyway to automatically loop through all pages and select 1 row from each page to create a new 2d array for each iteration, without having to manually specify how many nested loops i need? The number of pages of my 3d array changes throughout my project so would be handy to not have to specify
example of my array : val(:,:,1) =
1 7
2 8
3 9
4 10
6 2
7 3
8 4
9 5
val(:,:,2) =
6 12
7 13
8 14
9 15
11 7
12 8
13 9
14 10
val(:,:,3) =
11 17
12 18
13 19
14 20
16 12
17 13
18 14
19 15
val(:,:,4) =
16 22
17 23
18 24
19 25
21 17
22 18
23 19
24 20
val(:,:,5) =
21 27
22 28
23 29
24 30
26 22
27 23
28 24
29 25
val(:,:,6) =
26 32
27 33
28 34
29 35
31 27
32 28
33 29
34 30

  0 Comments

Sign in to comment.

Answers (1)

Ahmet Cecen
Ahmet Cecen on 16 Mar 2018
Why not just do val(1,:,:) or val(i,:,:)?

  3 Comments

Thomas McLean
Thomas McLean on 16 Mar 2018
i want to loop through all the possible combinations of rows. its for an optimisation problem so i test each possible combination of one row from each page. But the number of pages of the 3d array can change depending on what i input at the start of the script.
Ahmet Cecen
Ahmet Cecen on 16 Mar 2018
Ah. This maybe intractable for large number of "pages" but you can always do something like:
CombList = combnk(1:size(Val,1),size(Val,3));
Which will you give you every row combination possible.
Now you can do a single loop to go through all iterations independent of the number of rows or pages.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by