|
"Danielle Botha" <delsatr1977@yahoo.com.au> wrote in message <iavfpm$rog$1@fred.mathworks.com>...
> Hi,
> I have a block of data that I wish to process.
> It has data then a set of zeros to signal then next block of data.
>
> The each data block I wish to average and return the average into a matrix.
> eg
> 1 3 5 6
> 4 5 7 2
> 0 0 0 0
> 0 0 0 0
> 2 3 4 5
> 5 8 8 9
> 0 0 0 0
> 0 0 0 0
>
> How do I count how many rows in the data block(it could vary)?So that I can sum the individual block and avearge it? and then skip to the next data block a do the same.
>
> I have tried for ,if loops but can't get the increments correct?
>
> for length of data
- - - - - - - - -
I assume here that if a row is not all zeros, that whole row will be included in a block for averaging purposes. That is, if you had an isolated block equal to [0 1 2 3], for example, you would consider its average as 6/4 and not 6/3.
With that assumption, do this:
p = ~all(A==0,2);
q = diff([false;p])>0;
q = cumsum(q(p));
B = accumarray(q,sum(A(p,:),2))./accumarray(q,size(A,2)*ones(size(q)));
B should be a vector with your block averages.
Note: I haven't had the opportunity to fully test this, so let me know if it doesn't work properly.
Roger Stafford
|