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

classic Classic list List threaded Threaded
10 messages Options
fujimoto2005 fujimoto2005
Reply | Threaded
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
<http://mailinglists.scilab.org/file/t497065/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
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
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
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
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
fujimoto2005 fujimoto2005
Reply | Threaded
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
Rafael Guerra Rafael Guerra
Reply | Threaded
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
fujimoto2005 fujimoto2005
Reply | Threaded
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
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
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
Hermes Hermes
Reply | Threaded
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
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: mean of elements corresponding element is "true"

Hermes,

With your solution all false elements (zeros) get averaged with the true ones.
The averaging should be done only over the true-entries.

Regards,
Rafael

-----Original Message-----
From: users [mailto:[hidden email]] On Behalf Of Hermes
Sent: Sunday, March 04, 2018 10:10 AM
To: [hidden email]
Subject: Re: [Scilab-users] mean of elements corresponding element is "true"

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
_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Rafael Guerra Rafael Guerra
Reply | Threaded
Open this post in threaded view
|

Re: mean of elements corresponding element is "true"

In reply to this post by Samuel GOUGEON


Indeed. This is basically what nanmean()  does, managing in addition cases where no components remain on a range (all "%F").

If all components are %F then the results in Scilab 6 for both solutions are %nan, which is convenient.

(Scilab 5 requires additional attention because of division by zero errors, though)

 


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users