Using ngrid to create a matrix listing all combinations of values from row vectors
    7 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
Hi, I have the following vectors
    A= [1 2 3 4];
    B= [1 2];
    C= [1 2 3 6 7 8];
    D= [1 3 6];
and I want to create a matrix 144x4 listing in each row 1x4 a possible combination of elements from the vectors above in the order A(i) B(j) C(h) D(k). I could use ndgrid, but I don't know how to create the final matrix listing in each row a specific combination of values.
0 Kommentare
Akzeptierte Antwort
  Andrei Bobrov
      
      
 am 9 Dez. 2013
        
      Bearbeitet: Andrei Bobrov
      
      
 am 9 Dez. 2013
  
      A= [1 2 3 4];
B= [1 2];
C= [1 2 3 6 7 8];
D= [1 3 6];
CC = {A,B,C,D};
n = cellfun(@numel,CC);
n1 = cumsum(n);
i0 = fullfact(n);
idx = bsxfun(@plus,i0,[0, n1(1:end-1)]);
v = cat(2,CC{:});
out = v(idx);
ndgrid instead fullfact
k = cellfun(@(x)1:x,n,'un',0);
[i4,i3,i2,i1] = ndgrid(k(end:-1:1));
i0 = [i1(:),i2(:),i3(:),i4(:)];
0 Kommentare
Weitere Antworten (1)
Siehe auch
Kategorien
				Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!