MATLAB: Repeat elements in matrix repeat elements in matrix/array 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] Best Answer 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,[]);endbOrm=4n=2^(m-1)b=cell2mat(arrayfun(@(x) reshape(repmat(1:2^x,n/numel(1:2^x),1),1,[]),(0:m-1)','un',0)) Related SolutionsMATLAB: Matrix Manipulation A = [1; 1; 3; 1; 2; 1; 1; 1; 3; 1; 1; 1; 1; 2; 1; 1; 1; 1; 1; 3; 1; 1; 1; 2; 1; 1; 1; 1; 1; 2; 2; 1; 1; 1; 2; 1; 1; 2; 1; 1; 1; 1; 1; 1; 3; 1; 1; 3; 2; 1; 1; 1; 1; 1; 1; 1; 2; 2; 4; 1; 1; 2; 1; 1; 1; 1; 2; 2; 1; 1; 1; 3];a1=find(A==2)-1; %find the index of the 2a2=find(A==3)-1; %find the index of the 3a2=repmat(a2,2,1) %duplicate the 3 indexesB=sort([a1;a2])'; %join and sort the solution, also transpose%I transposed B because of your B example MATLAB: Repeat a matrix in defined manner A=[1 2 3;4 5 6;4 5 6];m=4B=cell2mat(arrayfun(@(x) x*ones(1,m),A,'un',0)) Related QuestionHow can a matrix of randperm be generated with variable n*mTake lines from arrayMaximizing efficiency in simple commandsHow can i split 30*9 matrix into 20*9 and 10*9Size(m,2) how the function worksHow to create on an efficient way zeros in a symbolic matrix
Best Answer