Averaging elements of a list of matrices over time
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
Suppose that I have a time series of a matrix, say, for 3 periods.
t1=0,3,0,6,0,0,3,6,0;
t2=0,6,0,3,0,3,0,6,0;
t3=0,3,6,3,0,6,3,3,0;
I want to take the average of each cell [i,j]
over time by considering the number of non-zero
elements only as follows:
I went through the documentation of Matrix Operations
but I could not find any thing close to what I describe above. Of course, one can use Flatten
, Sum
and Partition
to accomplish what I want. However, I like to have a more elegant way to do the job using matrix structure.
matrix
add a comment |Â
up vote
2
down vote
favorite
Suppose that I have a time series of a matrix, say, for 3 periods.
t1=0,3,0,6,0,0,3,6,0;
t2=0,6,0,3,0,3,0,6,0;
t3=0,3,6,3,0,6,3,3,0;
I want to take the average of each cell [i,j]
over time by considering the number of non-zero
elements only as follows:
I went through the documentation of Matrix Operations
but I could not find any thing close to what I describe above. Of course, one can use Flatten
, Sum
and Partition
to accomplish what I want. However, I like to have a more elegant way to do the job using matrix structure.
matrix
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
Suppose that I have a time series of a matrix, say, for 3 periods.
t1=0,3,0,6,0,0,3,6,0;
t2=0,6,0,3,0,3,0,6,0;
t3=0,3,6,3,0,6,3,3,0;
I want to take the average of each cell [i,j]
over time by considering the number of non-zero
elements only as follows:
I went through the documentation of Matrix Operations
but I could not find any thing close to what I describe above. Of course, one can use Flatten
, Sum
and Partition
to accomplish what I want. However, I like to have a more elegant way to do the job using matrix structure.
matrix
Suppose that I have a time series of a matrix, say, for 3 periods.
t1=0,3,0,6,0,0,3,6,0;
t2=0,6,0,3,0,3,0,6,0;
t3=0,3,6,3,0,6,3,3,0;
I want to take the average of each cell [i,j]
over time by considering the number of non-zero
elements only as follows:
I went through the documentation of Matrix Operations
but I could not find any thing close to what I describe above. Of course, one can use Flatten
, Sum
and Partition
to accomplish what I want. However, I like to have a more elegant way to do the job using matrix structure.
matrix
matrix
asked 56 mins ago
Tugrul Temel
525113
525113
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Quiet @ Block[Indeterminate = 0, Total[#] / Total[Unitize @ #]] & @ t1, t2, t3
% // MatrixForm // TeXForm
$left(
beginarrayccc
0 & 4 & 6 \
4 & 0 & frac92 \
3 & 5 & 0 \
endarray
right)$
Also
Total[t1, t2, t3] / (Total[Unitize @ t1, t2, t3] /. 0 -> 1)
same result
Your answers are perfect, just the type of answers I was thinking of. The first answer looks more elegant, though.
â Tugrul Temel
30 mins ago
Thank you @TugrulTemel for the accept and kind words.
â kglr
28 mins ago
2
First solution gives a warning on M11.3 Power::infy: Infinite expression 1/0 encountered.
â Okkes Dulgerci
24 mins ago
Thank you @OkkesDulgerci. Forgot toQuiet
.
â kglr
19 mins ago
@kglr: Yes, I was going to ask why we receive warnings from the first method. There is no room for warning because if all elements are zero, then the average is zero.
â Tugrul Temel
18 mins ago
 |Â
show 1 more comment
up vote
2
down vote
Total[t1, t2, t3]/Clip[Total[Unitize@t1, t2, t3], 1, Infinity]
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Quiet @ Block[Indeterminate = 0, Total[#] / Total[Unitize @ #]] & @ t1, t2, t3
% // MatrixForm // TeXForm
$left(
beginarrayccc
0 & 4 & 6 \
4 & 0 & frac92 \
3 & 5 & 0 \
endarray
right)$
Also
Total[t1, t2, t3] / (Total[Unitize @ t1, t2, t3] /. 0 -> 1)
same result
Your answers are perfect, just the type of answers I was thinking of. The first answer looks more elegant, though.
â Tugrul Temel
30 mins ago
Thank you @TugrulTemel for the accept and kind words.
â kglr
28 mins ago
2
First solution gives a warning on M11.3 Power::infy: Infinite expression 1/0 encountered.
â Okkes Dulgerci
24 mins ago
Thank you @OkkesDulgerci. Forgot toQuiet
.
â kglr
19 mins ago
@kglr: Yes, I was going to ask why we receive warnings from the first method. There is no room for warning because if all elements are zero, then the average is zero.
â Tugrul Temel
18 mins ago
 |Â
show 1 more comment
up vote
3
down vote
accepted
Quiet @ Block[Indeterminate = 0, Total[#] / Total[Unitize @ #]] & @ t1, t2, t3
% // MatrixForm // TeXForm
$left(
beginarrayccc
0 & 4 & 6 \
4 & 0 & frac92 \
3 & 5 & 0 \
endarray
right)$
Also
Total[t1, t2, t3] / (Total[Unitize @ t1, t2, t3] /. 0 -> 1)
same result
Your answers are perfect, just the type of answers I was thinking of. The first answer looks more elegant, though.
â Tugrul Temel
30 mins ago
Thank you @TugrulTemel for the accept and kind words.
â kglr
28 mins ago
2
First solution gives a warning on M11.3 Power::infy: Infinite expression 1/0 encountered.
â Okkes Dulgerci
24 mins ago
Thank you @OkkesDulgerci. Forgot toQuiet
.
â kglr
19 mins ago
@kglr: Yes, I was going to ask why we receive warnings from the first method. There is no room for warning because if all elements are zero, then the average is zero.
â Tugrul Temel
18 mins ago
 |Â
show 1 more comment
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Quiet @ Block[Indeterminate = 0, Total[#] / Total[Unitize @ #]] & @ t1, t2, t3
% // MatrixForm // TeXForm
$left(
beginarrayccc
0 & 4 & 6 \
4 & 0 & frac92 \
3 & 5 & 0 \
endarray
right)$
Also
Total[t1, t2, t3] / (Total[Unitize @ t1, t2, t3] /. 0 -> 1)
same result
Quiet @ Block[Indeterminate = 0, Total[#] / Total[Unitize @ #]] & @ t1, t2, t3
% // MatrixForm // TeXForm
$left(
beginarrayccc
0 & 4 & 6 \
4 & 0 & frac92 \
3 & 5 & 0 \
endarray
right)$
Also
Total[t1, t2, t3] / (Total[Unitize @ t1, t2, t3] /. 0 -> 1)
same result
edited 19 mins ago
answered 47 mins ago
kglr
167k8189392
167k8189392
Your answers are perfect, just the type of answers I was thinking of. The first answer looks more elegant, though.
â Tugrul Temel
30 mins ago
Thank you @TugrulTemel for the accept and kind words.
â kglr
28 mins ago
2
First solution gives a warning on M11.3 Power::infy: Infinite expression 1/0 encountered.
â Okkes Dulgerci
24 mins ago
Thank you @OkkesDulgerci. Forgot toQuiet
.
â kglr
19 mins ago
@kglr: Yes, I was going to ask why we receive warnings from the first method. There is no room for warning because if all elements are zero, then the average is zero.
â Tugrul Temel
18 mins ago
 |Â
show 1 more comment
Your answers are perfect, just the type of answers I was thinking of. The first answer looks more elegant, though.
â Tugrul Temel
30 mins ago
Thank you @TugrulTemel for the accept and kind words.
â kglr
28 mins ago
2
First solution gives a warning on M11.3 Power::infy: Infinite expression 1/0 encountered.
â Okkes Dulgerci
24 mins ago
Thank you @OkkesDulgerci. Forgot toQuiet
.
â kglr
19 mins ago
@kglr: Yes, I was going to ask why we receive warnings from the first method. There is no room for warning because if all elements are zero, then the average is zero.
â Tugrul Temel
18 mins ago
Your answers are perfect, just the type of answers I was thinking of. The first answer looks more elegant, though.
â Tugrul Temel
30 mins ago
Your answers are perfect, just the type of answers I was thinking of. The first answer looks more elegant, though.
â Tugrul Temel
30 mins ago
Thank you @TugrulTemel for the accept and kind words.
â kglr
28 mins ago
Thank you @TugrulTemel for the accept and kind words.
â kglr
28 mins ago
2
2
First solution gives a warning on M11.3 Power::infy: Infinite expression 1/0 encountered.
â Okkes Dulgerci
24 mins ago
First solution gives a warning on M11.3 Power::infy: Infinite expression 1/0 encountered.
â Okkes Dulgerci
24 mins ago
Thank you @OkkesDulgerci. Forgot to
Quiet
.â kglr
19 mins ago
Thank you @OkkesDulgerci. Forgot to
Quiet
.â kglr
19 mins ago
@kglr: Yes, I was going to ask why we receive warnings from the first method. There is no room for warning because if all elements are zero, then the average is zero.
â Tugrul Temel
18 mins ago
@kglr: Yes, I was going to ask why we receive warnings from the first method. There is no room for warning because if all elements are zero, then the average is zero.
â Tugrul Temel
18 mins ago
 |Â
show 1 more comment
up vote
2
down vote
Total[t1, t2, t3]/Clip[Total[Unitize@t1, t2, t3], 1, Infinity]
add a comment |Â
up vote
2
down vote
Total[t1, t2, t3]/Clip[Total[Unitize@t1, t2, t3], 1, Infinity]
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Total[t1, t2, t3]/Clip[Total[Unitize@t1, t2, t3], 1, Infinity]
Total[t1, t2, t3]/Clip[Total[Unitize@t1, t2, t3], 1, Infinity]
answered 33 mins ago
Alan
6,0541124
6,0541124
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f184641%2faveraging-elements-of-a-list-of-matrices-over-time%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password