MATLAB: How to put only one element randomly in row of matrix random nearest Hello, Can anyone help me please, I didn't a solution I'd like to put only one element of row to 1 'randomly' and the other putted in 0 Exemple I have A= zeros (4,3) I like to get A= [0 1 0; 1 0 0; 0 0 1; 1 0 0] Best Answer A = zeros(4,3) ; [m,n] = size(A) ;for i = 1:m idx = randperm(n,1) ; A(i,idx) = 1 ;end Related SolutionsMATLAB: Put matrix in cell Simpler:>> T = [3,6,1,12,7,10,6,0,0,22,0,15;2,4,4,3,2,4,5,0,0,20,0,4;0,1,0,2,0,2,0,0,0,17,0,16;0,5,0,6,0,7,0,0,0,6,0,0;0,0,0,0,0 28,0,0,0,0,0,0]T = 3 6 1 12 7 10 6 0 0 22 0 15 2 4 4 3 2 4 5 0 0 20 0 4 0 1 0 2 0 2 0 0 0 17 0 16 0 5 0 6 0 7 0 0 0 6 0 0 0 0 0 0 0 28 0 0 0 0 0 0>> F = @(v)v(v~=0).';>> C = cellfun(F,num2cell(T,1),'uni',0);>> C{:}ans = 3 2ans = 6 4 1 5ans = 1 4ans = 12 3 2 6ans = 7 2ans = 10 4 2 7 28ans = 6 5ans = []ans = []ans = 22 20 17 6ans = []ans = 15 4 16 MATLAB: Put number in cell B = num2cell(~cellfun('isempty',A) * .2) Related QuestionHow to put a matrix into an cell elementHow to put all the element in multidimesional arrayPut label in colorbarPut block behind colorbar
Best Answer