1

I have an array of data that is in variable m1 that is a 288 X 13 array. I need to average 24 rows in sequence 12 times for each column. I have tried many times to use this index we by calculating an average but I'm getting incorrect answers. My desired outcome (meanf1) is a 12 X 13 array showing the averages in 12 rows for each column of data. Thanks!

we = 1    25    49    73    97   121   145   169   193   217   241   265

    for ii = 1:13;
        for jj = 1:12;
            meanf1(jj,ii) = mean(m1(we(jj):we(jj)+23,ii));
        end;
    end`
Adriaan
  • 17,741
  • 7
  • 42
  • 75
user2100039
  • 1,280
  • 2
  • 16
  • 31
  • I don't understand what you want. Can you show us a small numerical example? – rayryeng Oct 15 '15 at 18:22
  • Can you show us why you think your answer is wrong? – Takeshi H. Oct 15 '15 at 18:23
  • the averages are off compared to when I do the average by hand or manually and sometimes i get zero's where there should be averages. I need to figure out a way to average 24 values then skip ahead to the next 24 values and take an average then skip ahead to the next 24 values and take an average and do this a total of 12 times. After doing this 12 times, I need to do the same thing to the next column and I have 13 columns. My needed outcome is a 12 row by 13 column array of averages. I hope that makes sense! Thank you!! – user2100039 Oct 15 '15 at 18:24
  • This code looks right. Could something wrong with your manual calculation? – Navan Oct 15 '15 at 18:28
  • the averaging is over 24 rows or values repeated 12 times. then, I need to repeat the averaging in the next column in the same way - averaging 24 rows at a time, repeated 12 times to row == 288 then moving to the next column. Desired outcome is a 12 row by 13 column of averages. Thanks! – user2100039 Oct 15 '15 at 18:32

3 Answers3

0

This should work:

for jj = 1:12    
    meanf1(jj,:) = mean(m1((floor((1:288)./24) == jj),:));    
end

one addition.... it depends on how you want to average.. you might also want to consider using ceil

meanf1(jj,:) = mean(m1((ceil((1:288)./24) == jj),:));
horseshoe
  • 1,437
  • 14
  • 42
0

Given the regular spaced intervals created by we, you can reshape and then use mean, like so -

meanf1 = reshape(mean(reshape(m1(1:288,1:13),24,[]),1),12,13);
Divakar
  • 218,885
  • 19
  • 262
  • 358
0

There are a few little mistakes here. At the top of my head the easiest way to adapt your version would be to change the jj to the numbers you wanted from "we". also you dont need to use 2 for loops as you can use one dimension as a vecot. This is one possible solution, yet i feel it is not the best.

    m1=rand(288,13);

    meanf1=ones(12,13); %preallocation is always better
    i=1;
    for jj = 1:24:265 ;
        meanf1(i,:) = mean(m1(jj:jj+23,:));
        i=i+1;
    end;
Finn
  • 2,333
  • 1
  • 10
  • 21