Trying to find all the average of negative numbers under certain conditions
    8 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Alexandra Brian
 el 4 de Feb. de 2017
  
    
    
    
    
    Editada: per isakson
      
      
 el 5 de Feb. de 2017
            Hi,
I'm trying to find the average of numbers less than 0 in one column that correspond with positive numbers in the rows of another column. I have the following code written. The avg for positives works, but the negatives return a positive number. There is obviously something going on. Could you please help? I attached the spreadsheet I used to this post.
clear all 
    xy= xlsread('xy_2.3.17.xlsx'); %Download spreadsheet to MATLAB
    x = xlsread('xy_2.3.17.xlsx','A:A'); %Choose row
    y = xlsread('xy_2.3.17.xlsx', 'B:B');%Choose row
    rows = xy(:,2)>0; %Logical vector with the rows which satisfy all conditions.
    if any(rows) % True if there is at least 1 row which meats the condition.
         avgx= mean(x(rows)); %Avg. when x > 0
     avgNeg = mean(y>0 & x<0); %Avg Negatives
0 comentarios
Respuesta aceptada
  per isakson
      
      
 el 4 de Feb. de 2017
        
      Editada: per isakson
      
      
 el 4 de Feb. de 2017
  
      Replace
avgNeg = mean(y>0 & x<0); %Avg Negatives
by
avgNeg = mean( x(y>0 & x<0) ); %Avg Negatives
2 comentarios
  per isakson
      
      
 el 5 de Feb. de 2017
				
      Editada: per isakson
      
      
 el 5 de Feb. de 2017
  
			Background
- Matlab has some different ways of indexing, see Matrix Indexing
- Matlab has many different data types, see Fundamental MATLAB Classes.
- Matlab converts between types when "appropriate", see Valid Combinations of Unlike Classes
This is "legal" with Matlab
>> 'a'+true+ones(true,true,'single')
ans =
    99
>>
>> mean([true,true,false])
ans =
    0.6667
With Matlab there is a tendency to save on keystrokes, write compact code and sacrifice on readability.
Intermediate Values says: "When calculating complex formulae, it is useful to break up the calculation into intermediate temporary variables."
Break up   mean( y>0 & x<0 )
is_y_gt_zero = y>0;                         % logical vector
is_x_lt_zero = x<0;                         % logical vector
is_avg_neg   = is_y_gt_zero & is_x_lt_zero; % logical vector
num_avg_neg  = double( is_avg_neg );        % double vector (type convertion)
avgNeg       = mean( num_avg_neg );         % double scalar
Break up   mean( x( y>0 & x<0 ) )
is_y_gt_zero = y>0;                         % logical vector
is_x_lt_zero = x<0;                         % logical vector
is_avg_neg   = is_y_gt_zero & is_x_lt_zero; % logical vector
num_avg_neg  = x( is_avg_neg );             % double vector (logical indexing)
avgNeg       = mean( num_avg_neg );         % double scalar
Execute these two and compare the values of the intermediate temporary variables.
Más respuestas (0)
Ver también
Categorías
				Más información sobre Matrix Indexing en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

