[討論] 請教N個for迴圈如何實現呢

作者: balancer12 (Do Something)   2022-12-05 22:37:56
想請教各位大大
我有一個變數A裡面可能有三個變異量
在A變異量裡面又各有兩個變數B
如此總共會有2*2*2=2^3=8組
寫法如下
GroupA_numPort=[ 1 2 3 ];
GroupB_CapVariable=[ 10e-6 100e-6 ];
iTest=0;
for i= 1:length(GroupB_CapVariable)
for j= 1:length(GroupB_CapVariable)
for k= 1:length(GroupB_CapVariable)
A_Port=[ GroupB_CapVariable(i) , GroupB_CapVariable(j) , GroupB_CapVaria
ble(k) ] ;
iTest=iTest+1;
end
end
end
因變數A有三個,必須寫三次for loop
若變數A變成5、10、99......
不太可能寫這麼多個for loop@@
請問有什麼方式可以實現N個for 迴圈嗎?
謝謝大家指教!
作者: sunev (Veritas)   2022-12-05 22:44:00
你迴圈內容和B無關嗎?看起來像是ndgrid
作者: balancer12 (Do Something)   2022-12-05 23:08:00
抱歉,修改一下寫的範例。主要是想可以產生出2*2*2=8組的組合出來

Links booklink

Contact Us: admin [ a t ] ucptt.com