How to assign few of the array values with a constant randomly .
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
chandra Naik
le 23 Juil 2019
Réponse apportée : chandra Naik
le 24 Juil 2019
I try with randperm but not getting an idea.
suppose n=10 and m=4;
require array with value '1' in 4 random position from 1 to 10 and othe postions value '0;
Expected result,
i.e array =[1, 0, 0, 1, 0, 0, 0, 0, 1,1] here m=4 and n=10
1 commentaire
Réponse acceptée
Stephen23
le 23 Juil 2019
Modifié(e) : Stephen23
le 23 Juil 2019
"How to assign few of the array values with a constant randomly ."
>> n = 10;
>> m = 4;
>> V = zeros(1,n); % generate array
>> X = randperm(n); % random indices
>> V(X(1:m)) = 1 % assign constant to some elements
V =
0 0 1 0 1 0 0 1 1 0
2 commentaires
Plus de réponses (2)
KALYAN ACHARJYA
le 23 Juil 2019
Modifié(e) : KALYAN ACHARJYA
le 23 Juil 2019
m=4;n=6;
A=[ones(1,m) zeros(1,n)];
[rows colm]=size(A);
colm_data=randperm(colm);
result=A(:,colm_data)
Please do change according as per your requirements (Minor Change)
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!