필터 지우기
필터 지우기

How to get possible combinations of variables?

조회 수: 3 (최근 30일)
Ajay Goyal
Ajay Goyal 2016년 2월 3일
편집: Stephen23 2016년 2월 3일
Dear Friends, I have got 20 vectors named (A1,A2...A20) with say 500 observations. I wanted to generate all possible combination. Moreover if A1A2 is a combination then it should also give values of A1*A2. Example,
A1 A2 A1A2
1 2 1*2
2 3 2*3
....
...
  댓글 수: 1
Stephen23
Stephen23 2016년 2월 3일
편집: Stephen23 2016년 2월 3일
Don't create twenty numbered variables. Although beginners keep doing this, using numbered variables leads to beginners writing slow, obfuscated, buggy programs. Here is why:
Note how the very first thing that Walter Roberson does in their comment (below) is to put all of these variables into one cell array, because this makes any processing of these variables much much easier. You should do the same: keep them in one variable, not twenty numbered ones.
The optimal storage would be in one numeric matrix.

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

답변 (1개)

Walter Roberson
Walter Roberson 2016년 2월 3일
  댓글 수: 2
Ajay Goyal
Ajay Goyal 2016년 2월 3일
Sorry It is not solving my purpose. All I want is a matrix with all possible combinations of given variables(A1...A20) with calculated values beneath them. Please guide me further with an example
Walter Roberson
Walter Roberson 2016년 2월 3일
You want to create variables A1A2, A1A3, and so on. And that is something you should avoid doing.
vars = {A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20};
nvars = length(vars);
results = cell(nvars, nvars);
for J = 1 : nvars - 1
results{J,J} = vars{K};
for K = J + 1 : nvars
t = vars{J}.*vars{K};
results{J,K} = t;
results{K,J} = t;
end
end
Now results{J,K} will be A_J .* A_K except that along the diagonal will be the original variables rather than the square of the variables.
Efficiency can be improved if the variables are known to be vectors, especially if their orientation is known ahead of time.

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by