Extracting multiple row values (with unequal range) in a column separately

I have a csv file 28526 x 1. These are a single variable for 45 years. I want to find the mean of 45 years (45 x1) seperately. But all 45 years unequal ranges. How to find the mean?
-----------------------------------------------------------------------------------
clc;clear all
a = readmatrix('His_wind.csv');
b = a(20279:20981, 1);
b(b == 9999) = NaN;
c = b/10;
d = mean(c,'omitnan');
--------------------------------------------------------------------------------------
Here I am selecting each range of values and calculating mean seperately for each year. Please advice on how to do it together.

2 个评论

how do you know the true range for each year? i doubt, can you provide the data file?
please find the data file attached. The header P DEW seperates each year

请先登录,再进行评论。

 采纳的回答

hello
try this :
clc;clear all
a = readmatrix('saf_wind.csv');
ind_header = find(isnan(a)); % lines with 'P DEW' (seperates each year)
% this for loop will do the yearly averaging on data between first and last
% detected lines with 'P DEW' - data before the first P DEW and data after
% the last P DEW mark are not computed
for ci =1:numel(ind_header)-1
start = ind_header(ci);
stop = ind_header(ci+1);
data = a(start:stop);
data(data > 9000) = NaN; % remove the 9999 from data (using > 9000 instead of == 9999 for potential rounding issues
d(ci) = mean(data/10,'omitnan');
end
plot(d)

4 个评论

FYI
an equivalent code without NaN acrobatics
clc;clear all
a = readmatrix('saf_wind.csv');
ind_header = find(isnan(a)); % lines with 'P DEW' (seperates each year)
% this for loop will do the yearly averaging on data between first and last
% detected lines with 'P DEW' - data before the first P DEW and data after
% the last P DEW mark are not computed
for ci =1:numel(ind_header)-1
start = ind_header(ci)+1;
stop = ind_header(ci+1)-1;
data = a(start:stop);
data(data > 9000) = []; % remove the 9999 from data (using > 9000 instead of == 9999 for potential rounding issues
d(ci) = mean(data/10);
end
plot(d)
Just figured out as you wanted the results for the 45 years, this means I had to add the computations for the data before the first and after the last mark 'P DEW'
this is it :
clc;clear all
a = readmatrix('saf_wind.csv');
ind_header = find(isnan(a)); % lines with 'P DEW' (seperates each year)
% this for loop will do the yearly averaging on data between first and last
% detected lines with 'P DEW' - data before the first P DEW and data after
% the last P DEW mark are not computed
for ci =1:numel(ind_header)-1
start = ind_header(ci)+1;
stop = ind_header(ci+1)-1;
data = a(start:stop);
data(data > 9000) = []; % remove the 9999 from data (using > 9000 instead of == 9999 for potential rounding issues
dd(ci) = mean(data/10);
end
% add the computation for the data before the first detected line with 'P DEW'
start = 1;
stop = ind_header(1)-1;
data = a(start:stop);
data(data > 9000) = []; % remove the 9999 from data (using > 9000 instead of == 9999 for potential rounding issues
d_first = mean(data/10);
% add the computation for the data afetr the last detected line with 'P DEW'
start = ind_header(end)+1;
data = a(start:end);
data(data > 9000) = []; % remove the 9999 from data (using > 9000 instead of == 9999 for potential rounding issues
d_last = mean(data/10);
% concatenation
d_final = [d_first dd d_last];

请先登录,再进行评论。

更多回答(1 个)

dat=readmatrix('saf_wind.csv');
dat(dat==9999)=[];
whereIsNan=find(isnan(dat));
yourMeans=zeros(1,numel(whereIsNan)+1);
for partNr=1:numel(whereIsNan)+1
if partNr==1
currVec=dat(1:whereIsNan(partNr(1))-1);
elseif partNr==numel(whereIsNan)+1
currVec=dat(whereIsNan(end)+1:end);
else
currVec=dat(whereIsNan(partNr-1)+1:whereIsNan(partNr)-1);
end
yourMeans(partNr)=mean(currVec);
end

1 个评论

Thanks a lot. This also works. Only I had to divide it by 10 as i needed the matrix in fraction.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Data Preprocessing 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by