필터 지우기
필터 지우기

Creating a row vector of combinations?

조회 수: 2 (최근 30일)
Ali Almakhmari
Ali Almakhmari 2023년 1월 24일
댓글: Dyuman Joshi 2023년 1월 27일
I have a row vector that 1 by 4. And I know that the minimum and maximum values of the elements in this vector to be -15 and 15. How can I make a matrix that is N by 4 that contains all possible combinations of ALL values?
Let me given an example: min = -15, max = 15, so the result of the code should be a matrix that is N by 4 that will look something like this
A = [[-15,0,0,0];[-14,0,0,0];[-13,0,0,0];[-12,0,0,0];[-11,0,0,0];[-10,0,0,0];[-9,0,0,0];....and so on until the final value vector to be[15,15,15,15]];

채택된 답변

Davide Masiello
Davide Masiello 2023년 1월 24일
You could use nchoosek. The example below is from -3 to 3 for memory issues (you'll generate a humongous matrix). Just substitute -15:15 and the trick is done.
A = nchoosek(-3:3,4)
A = 35×4
-3 -2 -1 0 -3 -2 -1 1 -3 -2 -1 2 -3 -2 -1 3 -3 -2 0 1 -3 -2 0 2 -3 -2 0 3 -3 -2 1 2 -3 -2 1 3 -3 -2 2 3
  댓글 수: 9
Dyuman Joshi
Dyuman Joshi 2023년 1월 25일
Nice work, Davide! I sometimes forget that FEx is an amazing resource as well
And the solution checks out, there should be 7^4 rows total for [-3 3]
7^4
ans = 2401
@Ali Almakhmari, you can obtain the desired solution for the intended range, but it would contain
fprintf('%d rows', 31^4)
923521 rows
so it would be difficult to go through all of them.
Ali Almakhmari
Ali Almakhmari 2023년 1월 25일
Thank you all for your help. You have been amazing!

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

추가 답변 (1개)

Stephen23
Stephen23 2023년 1월 25일
One simple aprpoach is to download this FEX submission:
but you will need plenty of memory:
V = -3:3
V = 1×7
-3 -2 -1 0 1 2 3
X = combinator(numel(V),4,'p','r');
M = V(X)
M = 2401×4
-3 -3 -3 -3 -3 -3 -3 -2 -3 -3 -3 -1 -3 -3 -3 0 -3 -3 -3 1 -3 -3 -3 2 -3 -3 -3 3 -3 -3 -2 -3 -3 -3 -2 -2 -3 -3 -2 -1
  댓글 수: 7
Stephen23
Stephen23 2023년 1월 26일
편집: Stephen23 2023년 1월 26일
"What do you mean by copy them?"
Perhaps COPYFILE or one of the WEB* or URL* functions lets you copy from the FEX location to the Answers environment, from whence you could run it. Go and read the documentation and do some experiments. I can't help you with this, I have never tried.
"Also, how did you run the function?"
I downloaded it from FEX, uploaded it here, ran the code, then (out of respect for the submitter's license conditions) deleted the function before submitting my answer. You can delete uploaded files by clicking on the little red "x":
Dyuman Joshi
Dyuman Joshi 2023년 1월 27일
Okay, I'll try it. Thanks for replying.
"I downloaded it from FEX, uploaded it here, ran the code, then (out of respect for the submitter's license conditions) deleted the function before submitting my answer."
Fair enough.

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by