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]

 Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 13 Jul. 2015
Bearbeitet: Azzi Abdelmalek am 13 Jul. 2015

0 Stimmen

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 Kommentare

ali sedeek
ali sedeek am 13 Jul. 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by