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]

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 13일
편집: Azzi Abdelmalek 2015년 7월 13일

0 개 추천

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

ali sedeek
ali sedeek 2015년 7월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2015년 7월 13일

댓글:

2015년 7월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by