MATLAB: How to create a matrix from given vectors matrixvector I have a vector A= [ 2 4 1 3 ] How can you create a matrix which are the length of the vector values with ones. the rest zeros? i.e I want B= [1 1 1 1; 1 1 0 1; 0 1 0 1; 0 1 0 0] Regards jason Best Answer C = zeros(max(A),numel(A));C(A + (0:numel(A)-1)*size(C,1)) = 1;B = flipud(cumsum(C(end:-1:1,:))); Related SolutionsMATLAB: Given two vectors A and B, what is the best way to generate the vector C such that sum(C==A(i)) = B(i) A = [1 2 3];B = [2 4 6];i2 = cumsum(B);idx = zeros(1,i2(end));idx(i2 - B + 1) = 1;C = A(cumsum(idx));orC = cell2mat(arrayfun(@(x,y)x(ones(1,y)),A,B,'un',0));ori2 = cumsum(B);i1 = i2 - B + 1;C = zeros(1,i2(end));for jj = 1:numel(A) C(i1(jj):i2(jj)) = repmat(A(jj),1,B(jj));end MATLAB: To generate alternate 0’s and 1’s out = repelem(mod(0:numel(A)-1,2),A); Related QuestionAverage of matrix elementHow to make all values before first non zero element in a matrix 1How to convert Cell Array index into Matrix with onesFor loop vector multiplication1D array values to 2D matrix
Best Answer