0

this will be very simple for some, but I could not find the answer on the website. Thanks in advance for your help. I have two vectors of the same length (each has 11 elements). I am trying to generate a matrix where each row of the matrix is a (different) combination of the elements of each vector, element wise.

Let's take an example with 3 element vectors, and see whether I can explain better. These are the vectors:

A = [a1 a2 a3]
B = [b1 b2 b3]

where a1, a2, a3, b1, b2, b3 are scalars.
What I would need Matlab to generate is a matrix with 8 rows and 3 columns with all combinations, element by element, so that the first elements of a row is always taken from the first element of the vector (either of the two vectors) like this:

 C = [a1 a2 a3;
      a1 a2 b3;
      a1 b2 a3;
      a1 b2 b3;
      b1 a2 a3;
      b1 a2 b3;
      b1 b2 a3;
      b1 b2 b3]

Since I will have 11 elements writing it down by hand is tedious. What the suggested link does is to create a matrix of pairs of values, which is not what I need. Thank you so much for your help!

EBH
  • 10,350
  • 3
  • 34
  • 59
Mic Bat
  • 1
  • 1
  • Thanks a lot for your comment. I do not see it as a duplicate, since I do not want a matrix with pairs, both rather a matrix with 11 elements, where each row of the matrix is a combination of taking one element from each of the two vectors. For example, all 11 elements are taken fro the first vector woudl be row 1, first 10 elements from the first vector, 11th element from the second vector would be row two, and so on... I am sorry I think I am not able to explain this well thanks a lot for your help! – Mic Bat Sep 27 '16 at 09:08
  • your example is confusing and your description is a little vague, if you believe it is not a duplicate, please provide a solid example of your problem so we can understand the difference. – GameOfThrows Sep 27 '16 at 09:12
  • Thanks, let's try. Let's take an example with 3 element vectors, and see whether I can explain better. These are the vectors: A=[a1 a2 a3] B=[b1 b2 b3] What I would need Matlab to generate is a matrix with 8 rows and 3 columns like this – Mic Bat Sep 27 '16 at 09:13
  • I see, so the first element of each vector is fixed, and you want the combinations of the second and third elements; @thewaywewalk I think this question is not an exact duplicate and may have some merits for answering. – GameOfThrows Sep 27 '16 at 09:22
  • The first element is not fixed either. In each row of the matrix, the first element is the first element of one of the vectors, the second is the second element is the second element of one of the vectors, the third element is the third element from one of the vectors. The matrix then has each of the possible combinations of this in each row. I will need a code to do this for an 11-element vectors as well so I think the code cannot be too brute force (for three elements, one could certainly brute force it) – Mic Bat Sep 27 '16 at 09:27
  • There will be 2^n rows in the matrix, so in the example given they will be 2^3=8 – Mic Bat Sep 27 '16 at 09:41
  • Can you please unmark it as duplicate? – Mic Bat Sep 27 '16 at 09:42
  • yes, I've casted the vote for reopen – GameOfThrows Sep 27 '16 at 09:49
  • I'm unable to add an answer, so am doing this in the comments... The following may not be elegant, and it does use the Communications Toolbox, but: `M=de2bi(0:2^length(A)-1); C=repmat(A,2^length(A)).*M+repmat(B,2^length(B)).*(1-M);` does what you need using a mask, M, to select between A and B... – Dave Sep 27 '16 at 10:12
  • hmmm, I have a easy way to do it, but I really want to properly format it as an answer, I will see if I can get some other guys to reopen this one. – GameOfThrows Sep 27 '16 at 10:25
  • Dave: I get an error it seems: Error using .* Matrix dimensions must agree. Trying to see why. Thanks a lot for your time though! – Mic Bat Sep 27 '16 at 11:33
  • 1
    I will give you my answer here, the first line you need is: `c=unique(nchoosek(repmat([1,2],[1,n]),n),'rows')` where n is the length of your vectors, this will give you a matrix of 1 and 2 which are the indexes. now you want to do: `(-(c-2).*repmat(a,[m,1]))+((c-1).*repmat(b,[m,1]))` where m = size(c,1) ; these 2 lines should give you the right answer – GameOfThrows Sep 27 '16 at 12:19
  • @GameOfThrows - I like this solution as it doesn't depend on the Communications Toolbox. Essentially it is doing what I did (except I forgot to put in the extra `,1)` in the repmats - sorry @Mic Bat. I can't think of any other way than generating some form of mask - c in your example and M in mine, that would make this more efficient. – Dave Sep 27 '16 at 12:51
  • It worked! You guys have been great thanks a lot!!! – Mic Bat Sep 27 '16 at 20:30

0 Answers0