how to calculate mean value in each row of data?
조회 수: 60 (최근 30일)
이전 댓글 표시
Hello everyone, i want to ask some help with my script.
I have a file with different number of column in each row, and i want to calculate the mean value of each row start from column 4 to end than write it in the new file.
This is my script
clear;
clc;
format short;
f = dir('D:\full_Data_Cryosat_c2p0004.txt');
readlines = @(F)regexp(fileread(F),'\r?\n','split');
for A = 1 : length(f)
D = f(A).name;
ff = D(:,19:25);
S = readlines(D);
data = cellfun(@(s) sscanf(s, '%f', [1 inf]), S);
mask = cellfun(@length, data) < 4;
data(mask) = []; %get rid of lines too short
referensi = cellfun(@(L) L(1), data);
lat = cellfun(@(L) L(2), data);
lon = cellfun(@(L) L(3), data);
means = cellfun(@(L) mean(L(4:end), 'omitnan'), data);
msl = [referensi(:), lat(:), lon(:), means(:)];
file_name = ['Msl_' ff];
dlmwrite(file_name, msl, 'delimiter', '\t');
end
But i'm facing an error like this
Error using cellfun
Non-scalar in Uniform output, at index 1, output 1.
Set 'UniformOutput' to false.
Error in script_msl (line 13)
data = cellfun(@(s) sscanf(s, '%f', [1 inf]), S);
Thanks for helping me
댓글 수: 0
채택된 답변
Cris LaPierre
2021년 10월 20일
Use mean and specify the dimension (1=down rows, 2=across columns)
댓글 수: 5
Cris LaPierre
2021년 10월 20일
Ah, I see you are using R2017a. The syntax was slightly different back then.
data = readtable('full_Data_Cryosat_c2p0004.txt','HeaderLines',0)
mData = mean(data{:,4:end},2,'omitnan')
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!