repeat element in an array

2 views (last 30 days)
HU Jianho
HU Jianho on 16 Jan 2012
Hello, If I have this matrix A = A = [1 1 1; 2 2 2; 3 3 3]; and I want to get this result C = [1 1 1; 1 1 2; 1 1 3; 1 2 1; 1 2 2; 1 2 3; 1 3 1; . . .; . . .; 3 3 1; 3 3 2; 3 3 3] I need the elements to be repeated to get the C matrix.
Please help me with that, thanks.

Answers (2)

Oleg Komarov
Oleg Komarov on 16 Jan 2012

Sean de Wolski
Sean de Wolski on 16 Jan 2012
One way:
[xx yy zz] = ndgrid(1:3);
C = [xx(:) yy(:) zz(:)]

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!