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

조회 수: 2 (최근 30일)
Thomas McLean
Thomas McLean 2018년 3월 16일
편집: Adam 2018년 3월 16일
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

답변 (1개)

Ahmet Cecen
Ahmet Cecen 2018년 3월 16일
Why not just do val(1,:,:) or val(i,:,:)?
  댓글 수: 3
Ahmet Cecen
Ahmet Cecen 2018년 3월 16일
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.
Thomas McLean
Thomas McLean 2018년 3월 16일
ah ok, i'll try that. Thankyou so much! :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by