how to put only one element randomly in row of matrix

1 次查看(过去 30 天)
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]

采纳的回答

KSSV
KSSV 2020-8-11
编辑:KSSV 2020-8-11
A = zeros(4,3) ;
[m,n] = size(A) ;
for i = 1:m
idx = randperm(n,1) ;
A(i,idx) = 1 ;
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by