MATLAB: Matrix with at most one 1 per row binarymatrix arrayone Hi, I want to create a binary 2D matrix with at most one '1' per row. This '1' should be at a random column in each row. Can anyone provide me help on this? thank you in advance Best Answer A=zeros(4)n=size(A,1)idy=randperm(n)idx=1:nidxy=sub2ind(size(A),idx,idy)A(idxy)=1If the matrix is not squareA=zeros(7,5)[n,m]=size(A);idy=randi(m,1,n)idx=1:nidxy=sub2ind(size(A),idx,idy)A(idxy)=1 Related SolutionsMATLAB: Shuffle n rows of a matrix Try thisM = rand(5);idx = [randperm(3) 4 5];M_shuffled = M(idx, :) MATLAB: Find the repetition of matrix rows a = [1 2 3; 4 5 6;1 2 3; 3 2 4; 1 2 3; 3 2 1; 1 3 2] ;row2check = 1;repeated = 0 ;for i = 1 : size(a,1) repeated = repeated + prod(double([a(row2check,:)== a(i,:)])) ;endfprintf('\n Row number %i is reapeated %i times in matrix!\n\n',row2check,repeated); Related QuestionHow to generate a matrix whose rows numbers are not elements of a given row vectorHow to can I create a square diagonal matrix and insert variables into itHow to find out whether there is a repetitive element in the columns of a matrixHow to convert A=[1 3 2] to B= [ 1 0 0;0 0 3;0 2 0]
Best Answer