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일

0 개 추천

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

Ali Almakhmari
Ali Almakhmari 2023년 1월 24일
I love you
Davide Masiello
Davide Masiello 2023년 1월 24일
XD my pleasure XD
Dyuman Joshi
Dyuman Joshi 2023년 1월 24일
This, however, will not generate permutations with repetitions as OP wants.
[-3 0 0 0], [-2 0 0 0], [3 3 3 3] etc will not be created.
Ali Almakhmari
Ali Almakhmari 2023년 1월 24일
Interesting. Any suggestions on how to incorporate them?
Dyuman Joshi
Dyuman Joshi 2023년 1월 24일
As of now, No. I am still thinking. I'll update you if and when I get an idea.
Davide Masiello
Davide Masiello 2023년 1월 25일
편집: Davide Masiello 2023년 1월 25일
@Dyuman Joshi thanks for pointing that out, I had missed it completely.
I think I came up with a solution, albeit slightly convoluted.
Some time ago I learned about a function on the File Exchange called allVL1.
It performs all combinations of integer vector that must verify a criteria on the sum.
I embedded this function in a loop to obtain the desired result.
The logic is as such.
1 - I define the maximum value, in this example 3 (or 15 in @Ali Almakhmari's problem).
2 - I establish the maximum sum achievable which is the maximum value times the number of columns of the output matrix (i.e., 3x4=12 in this case).
3 - Then, I loop for every combination leading to a sum of the row vector that goes from 0 to the maximum achievable sum and I concatenate the result to matrix A each time.
4 - Since this is done for positive integers, after the loop I concatenate -A with A to make it symmetric around zero.
5 - I perform a unique operation to remove the duplicated [0 0 0 0] row.
Please find the result attached below (full A matrix).
I still supect there might be a much easier way to do this.
maxN = 3;
ncol = 4;
A = [];
for sum = 0:maxN*ncol
a = allVL1(ncol,sum);
a = a(all(a<=maxN,2),:);
A = [A;a];
end
A = [-A;A]
A = [
0 0 0 -1
0 0 -1 0
0 -1 0 0
-1 0 0 0
0 0 0 -2
0 0 -1 -1
0 0 -2 0
0 -1 0 -1
0 -1 -1 0
0 -2 0 0
-1 0 0 -1
-1 0 -1 0
-1 -1 0 0
-2 0 0 0
0 0 0 -3
0 0 -1 -2
0 0 -2 -1
0 0 -3 0
0 -1 0 -2
0 -1 -1 -1
0 -1 -2 0
0 -2 0 -1
0 -2 -1 0
0 -3 0 0
-1 0 0 -2
-1 0 -1 -1
-1 0 -2 0
-1 -1 0 -1
-1 -1 -1 0
-1 -2 0 0
-2 0 0 -1
-2 0 -1 0
-2 -1 0 0
-3 0 0 0
0 0 -1 -3
0 0 -2 -2
0 0 -3 -1
0 -1 0 -3
0 -1 -1 -2
0 -1 -2 -1
0 -1 -3 0
0 -2 0 -2
0 -2 -1 -1
0 -2 -2 0
0 -3 0 -1
0 -3 -1 0
-1 0 0 -3
-1 0 -1 -2
-1 0 -2 -1
-1 0 -3 0
-1 -1 0 -2
-1 -1 -1 -1
-1 -1 -2 0
-1 -2 0 -1
-1 -2 -1 0
-1 -3 0 0
-2 0 0 -2
-2 0 -1 -1
-2 0 -2 0
-2 -1 0 -1
-2 -1 -1 0
-2 -2 0 0
-3 0 0 -1
-3 0 -1 0
-3 -1 0 0
0 0 -2 -3
0 0 -3 -2
0 -1 -1 -3
0 -1 -2 -2
0 -1 -3 -1
0 -2 0 -3
0 -2 -1 -2
0 -2 -2 -1
0 -2 -3 0
0 -3 0 -2
0 -3 -1 -1
0 -3 -2 0
-1 0 -1 -3
-1 0 -2 -2
-1 0 -3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 3 0
2 2 0 2
2 2 1 1
2 2 2 0
2 3 0 1
2 3 1 0
3 0 0 3
3 0 1 2
3 0 2 1
3 0 3 0
3 1 0 2
3 1 1 1
3 1 2 0
3 2 0 1
3 2 1 0
3 3 0 0
0 1 3 3
0 2 2 3
0 2 3 2
0 3 1 3
0 3 2 2
0 3 3 1
1 0 3 3
1 1 2 3
1 1 3 2
1 2 1 3
1 2 2 2
1 2 3 1
1 3 0 3
1 3 1 2
1 3 2 1
1 3 3 0
2 0 2 3
2 0 3 2
2 1 1 3
2 1 2 2
2 1 3 1
2 2 0 3
2 2 1 2
2 2 2 1
2 2 3 0
2 3 0 2
2 3 1 1
2 3 2 0
3 0 1 3
3 0 2 2
3 0 3 1
3 1 0 3
3 1 1 2
3 1 2 1
3 1 3 0
3 2 0 2
3 2 1 1
3 2 2 0
3 3 0 1
3 3 1 0
0 2 3 3
0 3 2 3
0 3 3 2
1 1 3 3
1 2 2 3
1 2 3 2
1 3 1 3
1 3 2 2
1 3 3 1
2 0 3 3
2 1 2 3
2 1 3 2
2 2 1 3
2 2 2 2
2 2 3 1
2 3 0 3
2 3 1 2
2 3 2 1
2 3 3 0
3 0 2 3
3 0 3 2
3 1 1 3
3 1 2 2
3 1 3 1
3 2 0 3
3 2 1 2
3 2 2 1
3 2 3 0
3 3 0 2
3 3 1 1
3 3 2 0
0 3 3 3
1 2 3 3
1 3 2 3
1 3 3 2
2 1 3 3
2 2 2 3
2 2 3 2
2 3 1 3
2 3 2 2
2 3 3 1
3 0 3 3
3 1 2 3
3 1 3 2
3 2 1 3
3 2 2 2
3 2 3 1
3 3 0 3
3 3 1 2
3 3 2 1
3 3 3 0
1 3 3 3
2 2 3 3
2 3 2 3
2 3 3 2
3 1 3 3
3 2 2 3
3 2 3 2
3 3 1 3
3 3 2 2
3 3 3 1
2 3 3 3
3 2 3 3
3 3 2 3
3 3 3 2
3 3 3 3 ];
Further thoughts.
The solution in my previous comment does not include combinations in which positive and negative integers appear in the same row vector.
This could be obtained with the following hack
maxN = 3;
ncol = 4;
A = [];
for sum = 0:2*maxN*ncol
a = allVL1(ncol,sum);
a = a(all(a<=2*maxN,2),:);
A = [A;a];
end
A = A-maxN
I am not posting the entire matrix again, its 2401 x 4.
Here's a snippet of it where both negative and positive integers appear at the same time.
A(500:505,:)
ans =
-3 1 -1 0
-3 1 0 -1
-3 1 1 -2
-3 1 2 -3
-3 2 -3 1
-3 2 -2 0
Moreover, we can check if the upper and lower bounds are correct
max(A)
ans =
3 3 3 3
min(A)
ans =
-3 -3 -3 -3
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일

3 개 추천

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

Dyuman Joshi
Dyuman Joshi 2023년 1월 25일
Does MATLAB Answers (and MATLAB Live Editor by extension) have the ability to directly run FEx functions/files?
Stephen23
Stephen23 2023년 1월 25일
"Does MATLAB Answers (and MATLAB Live Editor by extension) have the ability to directly run FEx functions/files?"
Not as far as I am aware. You might be able to copy them automatically, if you know the URL.
Ali Almakhmari
Ali Almakhmari 2023년 1월 25일
Thank you all for your help. You have been amazing!
Dyuman Joshi
Dyuman Joshi 2023년 1월 25일
"You might be able to copy them automatically, if you know the URL."
What do you mean by copy them? Also, how did you run the function?
Davide Masiello
Davide Masiello 2023년 1월 25일
I have the same question.
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.

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

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2023년 1월 24일

댓글:

2023년 1월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by