Filter löschen
Filter löschen

how to put only one element randomly in row of matrix

3 Ansichten (letzte 30 Tage)
Meriem Ben Kalia
Meriem Ben Kalia am 11 Aug. 2020
Kommentiert: Meriem Ben Kalia am 11 Aug. 2020
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]

Akzeptierte Antwort

KSSV
KSSV am 11 Aug. 2020
Bearbeitet: KSSV am 11 Aug. 2020
A = zeros(4,3) ;
[m,n] = size(A) ;
for i = 1:m
idx = randperm(n,1) ;
A(i,idx) = 1 ;
end

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by