How to find mean of selected values in a table with row gap???

7 views (last 30 days)
Hello,
I have a 98816x1 row vs column table stored in a variable. I have to calculate the mean of values in such a way: Averaging ROW1, ROW513, ROW1025 and so on (every 512 row gap). Similarly ROW2, ROW514, ROW1026 and so on. So the mean will be 512x1 table in the end.
the first mean I want is = (A(1)+A(513)+....)/193
the first mean I want is = (A(2)+A(514)+....)/193
Please help!

Accepted Answer

Image Analyst
Image Analyst on 26 Feb 2020
This really should be in the FAQ since we see this question so often. But the standard trick is to reshape the vector into a 2-D array and then take the mean across the proper direction of the array. Doing it along the proper direction is the key - very important. This will do the trick:
% Generate some sample data.
A = randi(9, 193*512, 1); % A 98816 row by 1 column vector.
% Take each run of 512 elements and put them into a separate column of a new matrix.
aReshaped = reshape(A, 512, []);
% Get the means in each row (going across columns).
% The first mean will be the mean of A(1), A(513), ... A(98305)
% The second mean will be the mean of A(2), A(514), ... A(98306)
% The last mean will be the mean of A(512), A(1025), ... A(98816)
theMeans = mean(aReshaped, 2);

More Answers (1)

KSSV
KSSV on 26 Feb 2020
Edited: KSSV on 26 Feb 2020
Let A be your 98816X1 column.
N = length(A) ;
n = 512 ;
B = reshape(A,n,[]) ;
iwant = mean(B) ;
  2 Comments
Image Analyst
Image Analyst on 26 Feb 2020
I don't think this is right. For the first set of means, the first mean = (A(1)+A(513))/2. He wants the second mean to be (A(1)+A(1025))/2. The third mean should be (A(1) + A(1537))/2. At least that's what he said. You should have 192 or 193 of these means.
Then repeat for the row below: First mean = (A(2)+A(514))/2. He wants the second mean to be (A(2)+A(1026))/2. The third mean should be (A(2) + A(1538))/2.
And so on down to the last set of means starting at A(511): First mean = (A(511)+A(511+512))/2. He wants the second mean to be (A(511)+A(511+2*512))/2. The third mean should be (A(511) + A(511+3*512))/2.
Farhan K
Farhan K on 26 Feb 2020
Edited: Farhan K on 26 Feb 2020
I am really sorry for the confusion, I have edited my question.
the 1st mean I want is = (A(1)+A(513)+....)/193
the 2nd mean I want is = (A(2)+A(514)+....)/193
and so on. Please advise!

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!