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]

 采纳的回答

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 个评论

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.

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by