Random number vector with only two values
    5 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Sania Gul
 am 23 Sep. 2024
  
    
    
    
    
    Kommentiert: Sania Gul
 am 23 Sep. 2024
            I want to generate 10 random numbers, but the value of these must be either 0 or pi=3.14. I have tried with randperm, randi, rand etc. but neither worked. Plz help me to resolve it.
0 Kommentare
Akzeptierte Antwort
  Stephen23
      
      
 am 23 Sep. 2024
        
      Bearbeitet: Stephen23
      
      
 am 23 Sep. 2024
  
      Indexing is a basic MATLAB superpower. You can easily use indexing to select from a vector of any values:
V = [0,pi];
W = V(randi(numel(V),1,10))
Multiplication also works for this special case with only two values, one of which is zero:
W = pi * randi([0,1],1,10)
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Creating and Concatenating Matrices 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!

