Matlab数据处理

130 篇文章 4 订阅
7 篇文章 0 订阅

线性拟合

%2次拟合,b是拟合系数,y = b1*x*x + b2*x + b3
b = polyfit(x, y, 2);

%拟合后y的新值
yy = polyval(b, x);

归一化

function out = data_normalize(in, min_percent, max_percent)
%{
in:输入数据
min_percent:最小百分比0~100
max_percent:最大百分比0~100
out:归一化后数据
%}
in = double(in);
out = in;

tmp = in;
nan_ind = isnan(tmp);
tmp(nan_ind) = [];
if isempty(tmp)
    return;
end

tmp = tmp(:);
min_data = prctile(tmp, min_percent);
max_data = prctile(tmp, max_percent);

if max_data == min_data
	out(~nan_ind) = 0.0;
    return;
end

out = (in-min_data)/(max_data-min_data);
out(out<0.0) = 0.0;
out(out>1.0) = 1.0;

均衡化

function out = histogramEqualization(in, scale)
%{
in:输入数据
scale:直方图格子数,256409665536等
out:直方图均衡化后数据
%}
in = double(in);
nan_ind = isnan(in);

out = in;

min_value = min(in(:));
max_value = max(in(:));
if max_value == min_value
    out(~nan_ind) = 0.0;
    return;
end

in = (in-min_value) / (max_value-min_value);
in = round(in*(scale-1)) + 1;

tmp = in;
tmp(nan_ind) = [];
len_tmp = numel(tmp);

cnt = zeros(scale, 1);
for i = 1 : len_tmp
    ind = tmp(i);
    cnt(ind) = cnt(ind) + 1;
end

for i = 2 : scale
    cnt(i) = cnt(i-1) + cnt(i);
end
f = cnt / len_tmp;

out = in;
in(nan_ind) = 1;

out(:) = f(in(:));
out(nan_ind) = nan;

网格化

function out = data_grid(in, lon, lat, width, height)
%{
in:散点数据
lon:散点经度
lat:散点纬度
width:宽度(~180°~180°划分格子数)
height:高度(90°~-90°划分格子数)
out:全球网格化数据

输入的散点in,lon,lat,需自行预处理剔除无效值。
网格化的区域数据,需自行计算起始行列号后裁剪out。
%}
%分配网格
cell_data = cell(height, width);

%默认nan值
out = nan(height, width);

%经纬度计算格子行列号
row = round( (height-1) * (90.0-lat) / 180.0 ) + 1;
col = round( (width-1) * (lon+180.0) / 360.0 ) + 1;

%遍历散点数据,并将序号放入对应格子中
for i = 1 : length(in)
    cur_row = row(i);
    cur_col = col(i);
    cell_data{cur_row, cur_col} = [cell_data{cur_row, cur_col}, i];
end

%遍历格子,并计算最终格子数据
for cur_row = 1 : height
    for cur_col = 1 : width
        if ~isempty(cell_data{cur_row, cur_col})
            ind = cell_data{cur_row, cur_col};
            %out(cur_row, cur_col) = mean(in(ind));  %平均值
            out(cur_row, cur_col) = median(in(ind)); %中位数
        end
    end
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值