gpt4 book ai didi

arrays - MatLab 瓶颈

转载 作者:太空宇宙 更新时间:2023-11-03 19:36:34 26 4
gpt4 key购买 nike

我正在处理大数组(~6x40 百万),我的代码显示出很大的瓶颈。我在 MatLab 编程经验丰富,但对内部过程(如内存等......)知之甚少。

我的代码如下(只是要点,当然所有变量都初始化了,特别是循环中的数组,我只是不想用代码轰炸你们):

首先我阅读了文件,

 disp('Point cloud import and subsampling')
tic
fid=fopen(strcat(Name,'.dat'));
C=textscan(fid, '%d%d%f%f%f%d'); %<= Big!
fclose(fid);

然后根据内容创建数组,

    y=C{1}(1:Subsampling:end)/Subsampling;
x=C{2}(1:Subsampling:end)/Subsampling;
%... and so on for the other rows

clear C %No one wants 400+ millon doubles just lying around.

并清除元胞数组 (1),并使用新值创建一些图像和数组

for i=1:length(x)

PCImage(y(i)+SubSize(1)-maxy+1,x(i)+1-minx)=Reflectanse(i);
PixelCoordinates(y(i)+SubSize(1)-maxy+1,x(i)+1-minx,:)=Coordinates(i,:);

end
toc

到这里为止一切都或多或少地运行得很顺利,但后来我操作了一些数组

disp('Overlap alignment')
tic

PCImage=PCImage(:,[1:maxx/2-Overlap,maxx/2:end-Overlap]); %-30 overlap?
PixelCoordinates=PixelCoordinates(:,[1:maxx/2-Overlap,maxx/2:end-Overlap],:);
Sphere=Sphere(:,[1:maxx/2-Overlap,maxx/2:end-Overlap],:);

toc

这是一个很大的瓶颈,但在下一步会变得更糟

disp('Planar view and point cloud matching')
tic

CompImage=zeros(max(SubSize(1),PCSize(1)),max(SubSize(2),PCSize(2)),3);

CompImage(1:SubSize(1),1:SubSize(2),2)=Subimage; %ExportImage Cyan
CompImage(1:SubSize(1),1:SubSize(2),3)=Subimage;
CompImage(1:PCSize(1),1:PCSize(2),1)=PCImage; %PointCloudImage Red

toc

输出

Point cloud import and subsampling

Elapsed time is 181.157182 seconds.

Overlap alignment

Elapsed time is 408.750932 seconds.

Planar view and point cloud matching

Elapsed time is 719.383807 seconds.

我的问题是:清除 1 中未使用的对象(如 C)会有什么影响吗? (好像不是这样的)

我是否在监督任何其他重要的机制或经验法则,或者整个事情是否太多了并且应该像这样发生?

最佳答案

当使用 subsref 时,matlab 会复制子引用的元素。这对于大型阵列来说可能代价高昂。通常像这样连接向量会更快

res = [a,b,c];

这对于上面编写的当前代码是不可能的,但如果可以修改代码以使其工作,则可能会节省一些时间。

编辑对于多维数组,您需要使用 cat

CompImage = cat(dim,Subimage,Subimage,PCImage);

在此示例中,dim 为 3。

关于arrays - MatLab 瓶颈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24011917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com