MATLAB: Replacing n random zeros in a matrix with a specific nonzero value MATLABreplacezeros Suppose I start with a zero matrix X = zeros(100) How can I replace n random zeros in matrix X with a single nonzero value (e.g., replace 10 zero elements in the matrix X with 1)? Best Answer X(randperm(numel(X), 10)) = 1 Related SolutionsMATLAB: Replace values in matrix A based on values of matrix B A(B==0) = 0; MATLAB: Displaying Specific Rows with Non-zero values Change your fprintf call to:fprintf(' Z%d%d %d\n',Ztitle(Ztitle(:,3)~=0,:).')That should do what you want. Related QuestionIn a matrix how to replace zero by NaN.Non-zero column valueGenerating a random symmetric matrixReplace zeros in a vector with previous nonzero numberI want to create a nxn matrix with all 0s and n randomly placed 1sReplace NaN with specific value.Replace diagonals in a matrixHow to chose random row which contain non-zero element
Best Answer