Averaging every 10 numbers in a matrix in Matlab
up vote
1
down vote
favorite
How to get an average of every 10 numbers in a big matrix (27x16800).
I can't find the solution
If anyone could help that would be great.
UPD Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row; I want to get a new matrix of 27 subjects with 1680 averaged numbers in rows (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number).
matlab matrix average
New contributor
|
show 4 more comments
up vote
1
down vote
favorite
How to get an average of every 10 numbers in a big matrix (27x16800).
I can't find the solution
If anyone could help that would be great.
UPD Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row; I want to get a new matrix of 27 subjects with 1680 averaged numbers in rows (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number).
matlab matrix average
New contributor
2
"Every 10 numbers" in what direction? What is your expected output size? How do you want to handle the first and last 9 numbers which won't have 10 numbers around them? Please show us a Minimal, Complete, and Verifiable example to make this question clearer.
– Wolfie
Nov 19 at 13:13
Hi @Vitto ! what do you mean exactly with "every ten numbers"? Along rows? along columns?
– Tommaso Di Noto
Nov 19 at 13:14
1
Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row, I want to get a new matrix of 27 subjects with 1680 averaged numbers (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number)
– Vitto Titto
Nov 19 at 13:47
2
You probably wantn = 10; result = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]);
wherex
is the data matrix
– Luis Mendo
Nov 19 at 14:30
Luis, Thank you so much! That is what I needed.
– Vitto Titto
Nov 19 at 14:41
|
show 4 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How to get an average of every 10 numbers in a big matrix (27x16800).
I can't find the solution
If anyone could help that would be great.
UPD Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row; I want to get a new matrix of 27 subjects with 1680 averaged numbers in rows (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number).
matlab matrix average
New contributor
How to get an average of every 10 numbers in a big matrix (27x16800).
I can't find the solution
If anyone could help that would be great.
UPD Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row; I want to get a new matrix of 27 subjects with 1680 averaged numbers in rows (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number).
matlab matrix average
matlab matrix average
New contributor
New contributor
edited Nov 19 at 13:52
New contributor
asked Nov 19 at 13:08
Vitto Titto
62
62
New contributor
New contributor
2
"Every 10 numbers" in what direction? What is your expected output size? How do you want to handle the first and last 9 numbers which won't have 10 numbers around them? Please show us a Minimal, Complete, and Verifiable example to make this question clearer.
– Wolfie
Nov 19 at 13:13
Hi @Vitto ! what do you mean exactly with "every ten numbers"? Along rows? along columns?
– Tommaso Di Noto
Nov 19 at 13:14
1
Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row, I want to get a new matrix of 27 subjects with 1680 averaged numbers (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number)
– Vitto Titto
Nov 19 at 13:47
2
You probably wantn = 10; result = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]);
wherex
is the data matrix
– Luis Mendo
Nov 19 at 14:30
Luis, Thank you so much! That is what I needed.
– Vitto Titto
Nov 19 at 14:41
|
show 4 more comments
2
"Every 10 numbers" in what direction? What is your expected output size? How do you want to handle the first and last 9 numbers which won't have 10 numbers around them? Please show us a Minimal, Complete, and Verifiable example to make this question clearer.
– Wolfie
Nov 19 at 13:13
Hi @Vitto ! what do you mean exactly with "every ten numbers"? Along rows? along columns?
– Tommaso Di Noto
Nov 19 at 13:14
1
Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row, I want to get a new matrix of 27 subjects with 1680 averaged numbers (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number)
– Vitto Titto
Nov 19 at 13:47
2
You probably wantn = 10; result = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]);
wherex
is the data matrix
– Luis Mendo
Nov 19 at 14:30
Luis, Thank you so much! That is what I needed.
– Vitto Titto
Nov 19 at 14:41
2
2
"Every 10 numbers" in what direction? What is your expected output size? How do you want to handle the first and last 9 numbers which won't have 10 numbers around them? Please show us a Minimal, Complete, and Verifiable example to make this question clearer.
– Wolfie
Nov 19 at 13:13
"Every 10 numbers" in what direction? What is your expected output size? How do you want to handle the first and last 9 numbers which won't have 10 numbers around them? Please show us a Minimal, Complete, and Verifiable example to make this question clearer.
– Wolfie
Nov 19 at 13:13
Hi @Vitto ! what do you mean exactly with "every ten numbers"? Along rows? along columns?
– Tommaso Di Noto
Nov 19 at 13:14
Hi @Vitto ! what do you mean exactly with "every ten numbers"? Along rows? along columns?
– Tommaso Di Noto
Nov 19 at 13:14
1
1
Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row, I want to get a new matrix of 27 subjects with 1680 averaged numbers (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number)
– Vitto Titto
Nov 19 at 13:47
Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row, I want to get a new matrix of 27 subjects with 1680 averaged numbers (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number)
– Vitto Titto
Nov 19 at 13:47
2
2
You probably want
n = 10; result = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]);
where x
is the data matrix– Luis Mendo
Nov 19 at 14:30
You probably want
n = 10; result = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]);
where x
is the data matrix– Luis Mendo
Nov 19 at 14:30
Luis, Thank you so much! That is what I needed.
– Vitto Titto
Nov 19 at 14:41
Luis, Thank you so much! That is what I needed.
– Vitto Titto
Nov 19 at 14:41
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
0
down vote
Given a matrix of random data x
:
x = randn(27,16800);
you can compute the average over all groups of n=10
values along the rows in two similar ways as described by Luis Mendo and Brice in comments:
y = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]); % Luis
y = squeeze(mean(reshape(x,size(x,1),n,),2)); % Brice
However, as noted by Wolfie, these work only if the length of the rows is exactly divisible by n
.
A more general approach can be obtained by convolving:
y = conv2(x,ones(1,n)/n,'valid');
y = y(:,1:n:end);
Each matrix element in the output of the convolution is the average over n
values. This result is n-1
elements shorter than the input. That is, we have computed n
times as many averages as needed. The second line takes the first of every n
averages, yielding an output of the expected size.
The convolution yields a result that is different from the other methods by numerical imprecision (max difference is 4.4409e-16 on my machine). This is because conv2
is implemented using SIMD instructions of your CPU, whereas mean
likely is not. The convolution approach might be somewhat slower than the other approach, but it is generic and easy to adapt.
Cris Luengo, thank you so much for the detailed explanation!
– Vitto Titto
Nov 20 at 19:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Given a matrix of random data x
:
x = randn(27,16800);
you can compute the average over all groups of n=10
values along the rows in two similar ways as described by Luis Mendo and Brice in comments:
y = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]); % Luis
y = squeeze(mean(reshape(x,size(x,1),n,),2)); % Brice
However, as noted by Wolfie, these work only if the length of the rows is exactly divisible by n
.
A more general approach can be obtained by convolving:
y = conv2(x,ones(1,n)/n,'valid');
y = y(:,1:n:end);
Each matrix element in the output of the convolution is the average over n
values. This result is n-1
elements shorter than the input. That is, we have computed n
times as many averages as needed. The second line takes the first of every n
averages, yielding an output of the expected size.
The convolution yields a result that is different from the other methods by numerical imprecision (max difference is 4.4409e-16 on my machine). This is because conv2
is implemented using SIMD instructions of your CPU, whereas mean
likely is not. The convolution approach might be somewhat slower than the other approach, but it is generic and easy to adapt.
Cris Luengo, thank you so much for the detailed explanation!
– Vitto Titto
Nov 20 at 19:41
add a comment |
up vote
0
down vote
Given a matrix of random data x
:
x = randn(27,16800);
you can compute the average over all groups of n=10
values along the rows in two similar ways as described by Luis Mendo and Brice in comments:
y = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]); % Luis
y = squeeze(mean(reshape(x,size(x,1),n,),2)); % Brice
However, as noted by Wolfie, these work only if the length of the rows is exactly divisible by n
.
A more general approach can be obtained by convolving:
y = conv2(x,ones(1,n)/n,'valid');
y = y(:,1:n:end);
Each matrix element in the output of the convolution is the average over n
values. This result is n-1
elements shorter than the input. That is, we have computed n
times as many averages as needed. The second line takes the first of every n
averages, yielding an output of the expected size.
The convolution yields a result that is different from the other methods by numerical imprecision (max difference is 4.4409e-16 on my machine). This is because conv2
is implemented using SIMD instructions of your CPU, whereas mean
likely is not. The convolution approach might be somewhat slower than the other approach, but it is generic and easy to adapt.
Cris Luengo, thank you so much for the detailed explanation!
– Vitto Titto
Nov 20 at 19:41
add a comment |
up vote
0
down vote
up vote
0
down vote
Given a matrix of random data x
:
x = randn(27,16800);
you can compute the average over all groups of n=10
values along the rows in two similar ways as described by Luis Mendo and Brice in comments:
y = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]); % Luis
y = squeeze(mean(reshape(x,size(x,1),n,),2)); % Brice
However, as noted by Wolfie, these work only if the length of the rows is exactly divisible by n
.
A more general approach can be obtained by convolving:
y = conv2(x,ones(1,n)/n,'valid');
y = y(:,1:n:end);
Each matrix element in the output of the convolution is the average over n
values. This result is n-1
elements shorter than the input. That is, we have computed n
times as many averages as needed. The second line takes the first of every n
averages, yielding an output of the expected size.
The convolution yields a result that is different from the other methods by numerical imprecision (max difference is 4.4409e-16 on my machine). This is because conv2
is implemented using SIMD instructions of your CPU, whereas mean
likely is not. The convolution approach might be somewhat slower than the other approach, but it is generic and easy to adapt.
Given a matrix of random data x
:
x = randn(27,16800);
you can compute the average over all groups of n=10
values along the rows in two similar ways as described by Luis Mendo and Brice in comments:
y = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]); % Luis
y = squeeze(mean(reshape(x,size(x,1),n,),2)); % Brice
However, as noted by Wolfie, these work only if the length of the rows is exactly divisible by n
.
A more general approach can be obtained by convolving:
y = conv2(x,ones(1,n)/n,'valid');
y = y(:,1:n:end);
Each matrix element in the output of the convolution is the average over n
values. This result is n-1
elements shorter than the input. That is, we have computed n
times as many averages as needed. The second line takes the first of every n
averages, yielding an output of the expected size.
The convolution yields a result that is different from the other methods by numerical imprecision (max difference is 4.4409e-16 on my machine). This is because conv2
is implemented using SIMD instructions of your CPU, whereas mean
likely is not. The convolution approach might be somewhat slower than the other approach, but it is generic and easy to adapt.
answered Nov 20 at 5:25
Cris Luengo
16.9k51847
16.9k51847
Cris Luengo, thank you so much for the detailed explanation!
– Vitto Titto
Nov 20 at 19:41
add a comment |
Cris Luengo, thank you so much for the detailed explanation!
– Vitto Titto
Nov 20 at 19:41
Cris Luengo, thank you so much for the detailed explanation!
– Vitto Titto
Nov 20 at 19:41
Cris Luengo, thank you so much for the detailed explanation!
– Vitto Titto
Nov 20 at 19:41
add a comment |
Vitto Titto is a new contributor. Be nice, and check out our Code of Conduct.
Vitto Titto is a new contributor. Be nice, and check out our Code of Conduct.
Vitto Titto is a new contributor. Be nice, and check out our Code of Conduct.
Vitto Titto is a new contributor. Be nice, and check out our Code of Conduct.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375345%2faveraging-every-10-numbers-in-a-matrix-in-matlab%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
"Every 10 numbers" in what direction? What is your expected output size? How do you want to handle the first and last 9 numbers which won't have 10 numbers around them? Please show us a Minimal, Complete, and Verifiable example to make this question clearer.
– Wolfie
Nov 19 at 13:13
Hi @Vitto ! what do you mean exactly with "every ten numbers"? Along rows? along columns?
– Tommaso Di Noto
Nov 19 at 13:14
1
Sorry, I should have been more clear. I have a matrix of recorded values (16800) for 27 subjects. Each subject corresponds to the row, I want to get a new matrix of 27 subjects with 1680 averaged numbers (whereas every "old" 10 numbers in rows will be averaged to 1 "new" mean number)
– Vitto Titto
Nov 19 at 13:47
2
You probably want
n = 10; result = permute(mean(reshape(x.', n, , size(x,1)), 1), [3 2 1]);
wherex
is the data matrix– Luis Mendo
Nov 19 at 14:30
Luis, Thank you so much! That is what I needed.
– Vitto Titto
Nov 19 at 14:41