MATLAB: Averaging from excel file

averageexcel

Hello! What I have is an excel file that looks something like this:
1 0 3 0
2 0 3 0
5 0 2 1
8 3 0 6
3 2 0 2
0 5 0 8
0 4 0 6
0 0 1 0
0 0 2 0
3 0 3 0
4 2 7 0
6 9 2 0
8 3 0 0
0 2 0 5
0 0 0 6
0 0 0 8
0 0 0 3
What I want to do is average the sections of non-zero numbers together in each column, so I would get a matrix like the following:
3.8 3.5 2.67 4.6
5.25 4.0 3.0 5.5
I've tried several different ways to no avail. Any help would be appreciated.

Best Answer

% Import data
data = xlsread('test.xlsx');
szData = size(data);
nblocks = 2;
% Loop per each column
out = zeros(nblocks,szData(2));
for c = 1:szData(2)
tmp = findseq(double(data(:,c) ~= 0));
out(1:end,c) = arrayfun(@(x) mean(data(tmp(x,2):tmp(x,3),c)),1:nblocks);
end
Findseq can be found here