Element-wise interval conformity check
    2 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    tompaine
 am 4 Mai 2015
  
    
    
    
    
    Bearbeitet: Stephen23
      
      
 am 4 Mai 2015
            Hello I have a Matrix
A=[1.0,2.0,0.7;1.2,3.0,1.0;0.8,1.1,0];
and an Interval
lower = 0.75;
upper = 1.25;
Matrix R should give me either 1 if the corresponding element of A is within the interval, or else 0:
R=[1,0,0;1,0,1;1,1,0];
Is there any way to do this without a loop?
0 Kommentare
Akzeptierte Antwort
  Stephen23
      
      
 am 4 Mai 2015
        
      Bearbeitet: Stephen23
      
      
 am 4 Mai 2015
  
      MATLAB lets you perform basic operations like this on the entire array (which is called code vectorization), so this comparison can be done directly using the relational operators, without any loops:
>> A = [1.0,2.0,0.7;1.2,3.0,1.0;0.8,1.1,0];
>> lwr = 0.75;
>> upr = 1.25;
>> R = lwr<=A & A<=upr
R =
   1     0     0
   1     0     1
   1     1     0
This is covered in the introduction tutorials to MATLAB:
0 Kommentare
Weitere Antworten (1)
  Purushottama Rao
      
 am 4 Mai 2015
        Without looping, this can be done by accessing each element of a matrix and processing with an if condition. That makes lot of hand written code compared to looping.
1 Kommentar
  Stephen23
      
      
 am 4 Mai 2015
				
      Bearbeitet: Stephen23
      
      
 am 4 Mai 2015
  
			This is misleading and very poor advice that shows a basic misunderstanding of how to use MATLAB. Simply reading the documentation for the relational operators makes it clear that they operate on whole arrays at once, for example le states " A <= B returns a logical array with elements set to logical 1 (true) where A is less than or equal to B..." note the word array in that sentence. The examples in the documentation also illustrate this:
A = [1 12 18 7 9 11 2 15];
A <= 12
ans =
   1     1     0     1     1     1     1     0
This means that no if statements or "lot of hand written code compared to looping" is required. Please learn about code vectorization.
Siehe auch
Kategorien
				Mehr zu Loops and Conditional Statements 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!


