repeat elements in matrix

how can i generate a matrix as shown [1 1 1 1 1 1 1 1; 1 1 1 1 2 2 2 2; 1 1 2 2 3 3 4 4; 1 2 3 4 5 6 7 8]

 Risposta accettata

Azzi Abdelmalek
Azzi Abdelmalek il 13 Lug 2015
Modificato: Azzi Abdelmalek il 13 Lug 2015
m=4;
n=2^(m-1);
b=zeros(m,n);
for k=0:m-1
a=1:2^k;
b(k+1,:)=reshape(repmat(a,n/numel(a),1),1,[]);
end
b
Or
m=4
n=2^(m-1)
b=cell2mat(arrayfun(@(x) reshape(repmat(1:2^x,n/numel(1:2^x),1),1,[]),(0:m-1)','un',0))

2 Commenti

ali sedeek
ali sedeek il 13 Lug 2015
thanks
What was wrong with
m = [1 1 1 1 1 1 1 1; 1 1 1 1 2 2 2 2; 1 1 2 2 3 3 4 4; 1 2 3 4 5 6 7 8];
It generates exactly what you asked for without assuming anything that you did not state. If you have some pattern, or generality in terms of size of dimensions, then you should have stated what it is.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by