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).










share|improve this question









New contributor




Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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]); 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















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).










share|improve this question









New contributor




Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 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]); 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













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).










share|improve this question









New contributor




Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question









New contributor




Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 19 at 13:52





















New contributor




Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 19 at 13:08









Vitto Titto

62




62




New contributor




Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Vitto Titto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 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]); 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














  • 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]); 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








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












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.






share|improve this answer





















  • Cris Luengo, thank you so much for the detailed explanation!
    – Vitto Titto
    Nov 20 at 19:41











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Vitto Titto is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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

























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.






share|improve this answer





















  • Cris Luengo, thank you so much for the detailed explanation!
    – Vitto Titto
    Nov 20 at 19:41















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.






share|improve this answer





















  • Cris Luengo, thank you so much for the detailed explanation!
    – Vitto Titto
    Nov 20 at 19:41













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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










Vitto Titto is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















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.















 


draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'