Matlab可以处理数据

发布于 2025-01-27 19:09:05 字数 1781 浏览 3 评论 0原文

我一直在处理855 4000x4000矩阵的处理2个列表。这是855个矩阵的列表,另一个是坐标列表(另外855 4000x4000矩阵)。在一个周期内执行此操作非常重要,以便不拥有成千上万的无用变量。对于每个文件,它都会剪切(读取不需要数据的NAN)协调数据,然后剪切与坐标相关的数据。然后,将所有值聚集到一个矩阵中。该代码是:

for x = 1:length(list_with_par)
    cd 'D:\Coord'
    par_lon = ncread(list_with_coordinates(x,:), 'longitude');
    par_lon(par_lon>=15) = nan;
    par_lon(par_lon<=-18) = nan;
    
    par_lat = ncread(list_with_coordinates(x,:), 'latitude');
    par_lat(par_lon>=84) = nan;
    par_lat(par_lon<=76) = nan;
    
    cd 'D:\Par'
    par = ncread(list_with_par(x,:), 'PAR');
    
    for i = 1:size(ncread(list_with_par(x,:),'PAR'),1) %size(,1)
        for z = 1:size(ncread(list_with_par(x,:),'PAR'),2) %size(,2)
            if isnan(par_lon(i,z))
                par(i,z) = nan;
            end
            if isnan(par_lat(i,z))
                par(i,z) = nan;
            end
        end
    end
    if size(par,2) < size(PAR_main,2)
        left_cells = size(PAR_main,2) - size(par,2);
        temp_cell = NaN(4865,left_cells);
        C2 = cat(2,par,temp_cell);
    end
    if size(par,2) == size(PAR_main,2)
        C2 = par(:,:,1);
    end
    PAR_main(:,:,x) = C2(:,:,1);
end

但是突然在处理4-5小时后会出现错误。

Error using netcdflib
The NetCDF library encountered an error during execution of 'open' function - 'HDF error (NC_EHDFERR)'.

Error in netcdf.open (line 67)
        [varargout{:}] = netcdflib ( 'open', filename, varargin{1} );

Error in internal.matlab.imagesci.nc/openToRead (line 1278)
            this.ncRootid = netcdf.open(this.Filename,'NOWRITE');

Error in internal.matlab.imagesci.nc (line 121)
                    this.openToRead();

Error in ncread (line 61)
ncObj   = internal.matlab.imagesci.nc(ncFile);

可能是一个问题?

I've been working on the processing 2 lists of 855 4000x4000 matrices. Here is a list of 855 matrices of some value, another one is a list of coordinates (another 855 4000x4000 matrices). It's important to do it within the one cycle, in order to not to have thousands of useless variables. For every file, it cuts (read put NaN where I don't need data) coordinates data, then it cuts data related to the coordinates. Then it gathers all the values into one matrix. The code is:

for x = 1:length(list_with_par)
    cd 'D:\Coord'
    par_lon = ncread(list_with_coordinates(x,:), 'longitude');
    par_lon(par_lon>=15) = nan;
    par_lon(par_lon<=-18) = nan;
    
    par_lat = ncread(list_with_coordinates(x,:), 'latitude');
    par_lat(par_lon>=84) = nan;
    par_lat(par_lon<=76) = nan;
    
    cd 'D:\Par'
    par = ncread(list_with_par(x,:), 'PAR');
    
    for i = 1:size(ncread(list_with_par(x,:),'PAR'),1) %size(,1)
        for z = 1:size(ncread(list_with_par(x,:),'PAR'),2) %size(,2)
            if isnan(par_lon(i,z))
                par(i,z) = nan;
            end
            if isnan(par_lat(i,z))
                par(i,z) = nan;
            end
        end
    end
    if size(par,2) < size(PAR_main,2)
        left_cells = size(PAR_main,2) - size(par,2);
        temp_cell = NaN(4865,left_cells);
        C2 = cat(2,par,temp_cell);
    end
    if size(par,2) == size(PAR_main,2)
        C2 = par(:,:,1);
    end
    PAR_main(:,:,x) = C2(:,:,1);
end

But suddenly an error pops up after 4-5 hours of processing.

Error using netcdflib
The NetCDF library encountered an error during execution of 'open' function - 'HDF error (NC_EHDFERR)'.

Error in netcdf.open (line 67)
        [varargout{:}] = netcdflib ( 'open', filename, varargin{1} );

Error in internal.matlab.imagesci.nc/openToRead (line 1278)
            this.ncRootid = netcdf.open(this.Filename,'NOWRITE');

Error in internal.matlab.imagesci.nc (line 121)
                    this.openToRead();

Error in ncread (line 61)
ncObj   = internal.matlab.imagesci.nc(ncFile);

What might be an issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

饮湿 2025-02-03 19:09:05

我并不真正熟悉ncread(和关联的功能),但是有两件事跳出我的效率很低:

  1. 在您的'i'和'z'中,是否有理由再次阅读数据以确定其大小,而不仅仅是使用您已经保存的“ PAR”变量?

    for i = 1:size(par,1)

    对于z = 1:size(par,2)

  2. 为此,除非我缺少针对此功能的特定内容,否则您应该能够跳过循环'i'和'z'完全矢量化计算:

    par(isnan(par_lon))= nan;

    par(isnan(par_lat))= nan;

这无疑会大大减慢您的代码。除此之外,很难说,但是我绝对可以看到有数百万个无关紧要的文件读取可能会引起临时文件地址等的某些问题)或内存泄漏。

I'm not really familiar with ncread (and associated functions), but there two things that jump out at me that appear to be very inefficient:

  1. In your loops over 'i' and 'z', is there a reason to read in the data again to determine its size instead of just using the 'par' variable that you already saved?

    for i = 1:size(par,1)

    for z = 1:size(par,2)

  2. For that matter, unless I am missing something specific to this set of functions, you should be able to skip the loops over 'i' and 'z' completely and vectorize the calculation:

    par(isnan(par_lon))= nan;

    par(isnan(par_lat)) = nan;

This is certainly significantly slowing down your code. It is hard to say beyond that, but I could definitely see how having millions of extraneous file reads could cause some issues with either temporary file addresses, etc.) or memory leaks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文