MATLAB: Can anyone help me to generate a matrix of 0’s and 1’s randomly in which each contains only one 1 like [ 0 1 0 0 0; 1 0 0 0 0; 0 0 0 1 0]. matrix array for example [0 1 0 0 0; 1 0 0 0 0; 0 0 0 1 0] Best Answer [~,ii] = sort(rand(3,5),2)out = ii == 1orout = zeros(3,5)[m,n] = size(out);out(sub2ind([m,n],1:m,randperm(n,m))) = 1 Related SolutionsMATLAB: Creating a large matrix with a pattern Hi bus14,Try this:m=zeros(804,200);m([1,2,4],:)=1;m2=reshape(m,800,[]);m2(:,201)=[]; MATLAB: I have a matrix [1 1 1 0; 0 1 1 0;1 1 0 1; 0 0 1 1] A = [1 1 1 0; 0 1 1 0;1 1 0 1; 0 0 1 1];B = reshape(A(1:end-1),3,5);BB = 1 0 1 1 0 0 1 0 0 0 1 1 1 1 1To get it as 5x3 is just changing the call to reshape. You need to make sure you know in which order you want the elements to be reshaped. Remember that MATLAB stores the elements of an array going down the columns. Related QuestionHow to place a cell array in another cell array using ‘find’I have a matrix A=[1 0 1 1;0 0 1 0; 0 0 1 0; 0 0 0 0] and I wanted to count the neighbor with value one of each pixel with value one. So I need to reach this result: result=[0 0 2 2;0 0 2 0;0 0 1 0;0 0 0 0]. I will be appreciated for your help.Cannot get the same result in GA as I get in linprog
Best Answer