how to calculate mean value in each row of data?

64 visualizaciones (últimos 30 días)
Retno Purwaningsih
Retno Purwaningsih el 20 de Oct. de 2021
Comentada: Retno Purwaningsih el 20 de Oct. de 2021
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

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 20 de Oct. de 2021
Use mean and specify the dimension (1=down rows, 2=across columns)
  5 comentarios
Cris LaPierre
Cris LaPierre el 20 de Oct. de 2021
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')
Retno Purwaningsih
Retno Purwaningsih el 20 de Oct. de 2021
yeasss that absolutely work !!! Thank youu so much for helping me ^^

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Cell Arrays en Help Center y File Exchange.

Productos


Versión

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by