# [Scilab-users] mean of elements corresponding element is "true"

10 messages
Open this post in threaded view
|

## [Scilab-users] mean of elements corresponding element is "true"

 In the attached code,"boolean_M" is a random Boolean matrix. I want to get the average of each column of A whose corresponding element of boolean_M has "true" as row vector x_V, but it does not work. Where am I wrong? Best regards test_meanWtboolean.sce   -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: mean of elements corresponding element is "true"

 Le 02/03/2018 à 16:40, fujimoto2005 a écrit : ```In the attached code,"boolean_M" is a random Boolean matrix. I want to get the average of each column of A whose corresponding element of boolean_M has "true" as row vector x_V, but it does not work. Where am I wrong?``` ```n=10; boolean_M=rand(n,n)>0.5; disp(boolean_M) A=rand(n,n); disp(A); x_V=mean(A(boolean_M),'r'); ``` Instead, you may use ```n = 5; boolean_M = rand(n,n)>0.5 A = grand(n,n,"uin",0,9) A(boolean_M) = %nan nanmean(A,"r")``` --> n = 5; --> boolean_M = rand(n,n)>0.5  boolean_M  =   T F F F T   T T T T T   T T F T T   F T F T F   F F T T F   --> A = grand(n,n,"uin",0,9)  A  =    6.   2.   5.   4.   6.    6.   9.   3.   6.   5.    3.   9.   3.   5.   4.    0.   8.   7.   0.   7.    1.   6.   8.   7.   2. --> A(boolean_M) = %nan  A  =    Nan   2.    5.    4.    Nan    Nan   Nan   Nan   Nan   Nan    Nan   Nan   3.    Nan   Nan    0.    Nan   7.    Nan   7.    1.    6.    Nan   Nan   2. --> nanmean(A,"r")  ans  =    0.5   4.   5.   4.   4.5 Samuel _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: mean of elements corresponding element is "true"

 Le 02/03/2018 à 16:54, Samuel Gougeon a écrit : Le 02/03/2018 à 16:40, fujimoto2005 a écrit : ```In the attached code,"boolean_M" is a random Boolean matrix. I want to get the average of each column of A whose corresponding element of boolean_M has "true" as row vector x_V, but it does not work. Where am I wrong?``` ```n=10; boolean_M=rand(n,n)>0.5; disp(boolean_M) A=rand(n,n); disp(A); x_V=mean(A(boolean_M),'r'); ``` Instead, you may use ```n = 5; boolean_M = rand(n,n)>0.5 A = grand(n,n,"uin",0,9) A(boolean_M) = %nan nanmean(A,"r")``` --> n = 5; --> boolean_M = rand(n,n)>0.5  boolean_M  =   T F F F T   T T T T T   T T F T T   F T F T F   F F T T F   --> A = grand(n,n,"uin",0,9)  A  =    6.   2.   5.   4.   6.    6.   9.   3.   6.   5.    3.   9.   3.   5.   4.    0.   8.   7.   0.   7.    1.   6.   8.   7.   2. --> A(boolean_M) = %nan or  --> A(~boolean_M) = %nan to stick to your requirement _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: mean of elements corresponding element is "true"

 Dear Samuel Thank you for your answer. Best regards -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: mean of elements corresponding element is "true"

 In reply to this post by Samuel GOUGEON An alternative solution: n=10; M= bool2s(rand(n,n)>0.5);  // Boolean to zero-ones matrix A= rand(n,n); x=  sum(A.*M,'r') ./ sum(M,'r');     Regards, Rafael _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: mean of elements corresponding element is "true"

 Dear Rafael, Thank you for another excellent solution. Best regards -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: mean of elements corresponding element is "true"

 In reply to this post by Rafael Guerra Le 03/03/2018 à 13:15, Rafael Guerra a écrit : An alternative solution: n=10; M= bool2s(rand(n,n)>0.5);  // Boolean to zero-ones matrix A= rand(n,n); x=  sum(A.*M,'r') ./ sum(M,'r'); Indeed. This is basically what nanmean()  does, managing in addtition cases where no components remain on a range (all "%F"). _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: mean of elements corresponding element is "true"

 In reply to this post by fujimoto2005 how about this way: n=10; boolean_M=rand(n,n)>0.5; disp(boolean_M) A=rand(n,n); disp(A); x_V=mean(boolean_M.*A,'r') disp(x_V) Hermes -- Sent from: http://mailinglists.scilab.org/Scilab-users-Mailing-Lists-Archives-f2602246.html_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users