フィルターのクリア

Generating a design matrix with for loops

2 ビュー (過去 30 日間)
Anders Larsen
Anders Larsen 2021 年 9 月 22 日
回答済み: Yongjian Feng 2021 年 9 月 22 日
Hello everyone, I am trying to create a design matrix for simulation puposes using for loops with vectors, but I am struggeling with what to write in the inner most loop. The numbers are arbitrary, to get the hang of it, I need to be able to scale it up for vectors with 100 values or more.
The code looks as follows:
a = (1: 3)';
b = (4: 6)';
c = (7: 9)';
for i = a
for j = b
for k = c
e(i, j k)=...
end
end
end
The result should be a matrix with all posibilities of combinations of the 3 vectors and should look like:
e = 1 4 7
1 4 8
1 4 9
1 5 7
1 5 8
1 5 9
1 6 7
1 6 8
1 6 9
2 4 7...
Hope you guys can help, thanks!

回答 (1 件)

Yongjian Feng
Yongjian Feng 2021 年 9 月 22 日
Something like this?
a = (1: 3);
b = (4: 6);
c = (7: 9);
idx = 1;
for i = a
for j = b
for k = c
e(idx, 1) = i;
e(idx, 2) = j;
e(idx, 3) = k;
idx = idx + 1;
end
end
end
e

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by