1. 前言
由于matlab内存限制,导致无法处理较大尺寸的矩阵;
2. 解决思路
读取原始大尺寸矩阵,分块后处理,及时删除中间过程文件,只保留分块处理后的最终结果,最后合并结果文件,减少内存占用。
3. 矩阵分割函数
function output_mat_blocks = mat_gap(input_mat, gap_num)% 进行矩阵分割,否则矩阵太大,超出内存无法处理% 参数说明:% 输入:input_mat 为输入的原始矩阵,gap_num 为进行行列分割的阈值% 输出:output_mat_blocks 为输出的分割后的矩阵分块,cell格式[rows, cols] = size(input_mat);% gap_num = 2000; % 设置分块大小阈值if rows > gap_num || cols > gap_numrow_gap_num = ceil(rows / gap_num);col_gap_num = ceil(cols / gap_num);row_block_size = floor(rows / row_gap_num);col_block_size = floor(cols / col_gap_num);% 输出分割行列row_gap = zeros(row_gap_num-1,1);col_gap = zeros(col_gap_num-1,1);for p=1:row_gap_num-1row_gap(p)=row_block_size*p;endfor q=1:col_gap_num-1col_gap(q)=col_block_size*q;end%…切割矩阵output_mat_blocks=cell(row_gap_num,col_gap_num);for i=1:row_gap_numfor j=1:col_gap_numif i~=row_gap_num && j~=col_gap_numoutput_mat_blocks{i,j}=input_mat(row_block_size*(i-1)+1:row_block_size*i,col_block_size*(j-1)+1:col_block_size*j);elseif i==row_gap_num && j~=col_gap_numoutput_mat_blocks{i,j}=input_mat(row_block_size*(i-1)+1:end,col_block_size*(j-1)+1:col_block_size*j);elseif i~=row_gap_num && j==col_gap_num output_mat_blocks{i,j}=input_mat(row_block_size*(i-1)+1:row_block_size*i,col_block_size*(j-1)+1:end);elseif i==row_gap_num && j==col_gap_num output_mat_blocks{i,j}=input_mat(row_block_size*(i-1)+1:end,col_block_size*(j-1)+1:end);endendendelseoutput_mat_blocks=input_mat;end
end
4. 矩阵合并函数
function recover_mat = merge_mat_blocks(output_mat_blocks, rows, cols)% 合并分割后的矩阵块,重建原始大矩阵% 参数说明:% 输入:output_mat_blocks 为分割后的矩阵分块,cell格式% rows 为原始矩阵的行数% cols 为原始矩阵的列数% 输出:reconstructed_mat 为合并后的原始矩阵if iscell(output_mat_blocks)% 获取分块的数量[row_gap_num, col_gap_num] = size(output_mat_blocks);% 计算每个分块的大小row_block_size = floor(rows / row_gap_num);col_block_size = floor(cols / col_gap_num);% 初始化一个空矩阵,大小与原始矩阵相同,填充0recover_mat = zeros(rows, cols);% 合并分块for i = 1:row_gap_numfor j = 1:col_gap_num% 计算当前分块在原始矩阵中的起始位置row_start = (i - 1) * row_block_size + 1;col_start = (j - 1) * col_block_size + 1;% 确定当前分块的大小if i == row_gap_numrow_end = rows;elserow_end = row_start + row_block_size - 1;endif j == col_gap_numcol_end = cols;elsecol_end = col_start + col_block_size - 1;end% 将分块数据复制到正确的位置recover_mat(row_start:row_end, col_start:col_end) = output_mat_blocks{i, j};endendelse% 如果没有分割,则 output_mat_blocks 就是原始矩阵recover_mat = output_mat_blocks;end
end
5. 调用
% 1. 栅格读取
[dem,dem_r]=readgeoraster('dem.tif'); % DEM数据文件
info=geotiffinfo('dem.tif');
dem_res=dem_r.CellExtentInWorldX;
[rows, cols]=size(dem);% 2. 进行矩阵分割
gap_num=3000;
dem_mat_blocks = mat_gap(dem, gap_num);
clear dem% 3. 进行矩阵分块处理
blocks_num = size(dem_mat_blocks,1)*size(dem_mat_blocks,2);
result=cell(size(dem_mat_blocks)); % 假如输出的结果为 result
for blocks_i=1:blocks_numdem=dem_mat_blocks{blocks_i};...; % 你对DEM的数据过程result{(blocks_i}=...;
end% 4. 合并结果矩阵(即result)
recover_mat = merge_mat_blocks(result, rows, cols);