Calculate mean for uneven weighting in matlab -
i want calculate mean of field of tracers in matlab, cells make field different size. example, tracer field is:
t = 1 3 5 8 2 1 4 3 2 1 9 1 20 8 3 1
and have 2 more fields, dx
, dy
describe size of cells make t
.
dx = 1 1 2 3 1 1 2 3 1 1 2 3 1 1 2 3
and
dy = 3 3 3 3 3 3 3 3 2 2 2 2 1 1 1 1
so, intuitively, dx
, dy
tell me bottom left hand corner of tracer field t
should have smallest contribution calculate of mean of t
, while top right hand corner should have greatest contribution.
i tried mean(mean(t))
, overweights importance of bottom left corner of t
, etc. after bit of investigation figured i'd thorough , calculate mean manually, , including weightings, using this:
t_mean_i = sum(t*dx)./sum(dx)
and similar dy
, cell width in y-direction. however, i'm not sure how implement this.
edit: here more detail question.
my grid 260*380 cells, size(dy) = size(dx) = 260-by-380
. tracer field calculated dividing surface flux field, sflux
sized salinity field, salt
. so, size(sflux) = size(salt) = 260-by-380-by-1000
, time dimension has length 1000
.
i want find mean of sflux(:,:)./salt(:,:,ii)
including weighting of cell width fields, dx
, dy
, @ each timestep ii
. (i won't use for-loop this, don't worry!!)
am on right track i'm doing? or think wrong? please feel free ask clarification.
cheers!
mean
, sum
operate along single dimension. apply them on entire matrix, convert data column vector first using colon operator (:
), example:
w = dx .* dy; sum(w(:))
to obtain desired mean each layer in 3-d array, like:
t = sflux ./ salt; %// tracer field w = dx .* dy; %// weights t_mean = sum(reshape(bsxfun(@times, t, w), [], size(t, 3))) / sum(w(:));
this produces array t_mean
elements corresponding mean values @ each time step.
explanation: bsxfun(@times, t, w)
multiples each layer element-wise weights. resulting weighted 3-d array reshaped 2-d array, each column converted different column (similar colon operator), , normalized sum of weights.
Comments
Post a Comment