Generating a design matrix with for loops

조회 수: 1 (최근 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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by