Averaging sections of a given column
Afficher commentaires plus anciens
Suppose I have a column vector A=[1 3 -999 4 9 8 -999 3 5]; I need to get average of each 3 sections. The problem is, if any section contains -999, I need to avoid it and get the average of rest of the numbers. For an example, for the 1st three numbers, average has to be (1+3)/2.
Please help me with that. Thanks.
3 commentaires
clear all
close all
A=[1 3 -999 4 9 8 -999 3 5]
index=find(A==-999)
A(index)=0;
sectionsize=3;
k=1;
for i=1:3:size(A,2)-2
out=A(i:i+2)
nzeros=numel(find(out==0));
output(k)=sum(out)/(sectionsize-nzeros);
k=k+1;
end
output
Thishan Dharshana Karandana Gamalathge
le 28 Juil 2017
Image Analyst
le 28 Juil 2017
Modifié(e) : Image Analyst
le 28 Juil 2017
That seems inadvisable. Why do you want nans in there instead of the very simple approach I showed you below?????
MSP, that should have been in the answer section below since you intended it as an answer rather than a comment (like asking for more information).
Réponses (1)
Image Analyst
le 28 Juil 2017
If you have the Image Processing Toolbox you can use regionprops():
A=[1 3 -999 4 9 8 -999 3 5]
props=regionprops(A~=-999, A, 'MeanIntensity');
sectionMeans = [props.MeanIntensity]
You get:
sectionMeans =
2 7 4
Catégories
En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!