Matlab 中的矢量化代码

发布于 2025-01-07 23:24:07 字数 1290 浏览 0 评论 0原文

我有一个 ASCII 文件,其中包含在地震解释软件中创建的许多表面。它们是顶面和底面之间的插值曲面族。所有曲面都在规则方形网格的每个点处采样。 文件结构为: XY Z1 Z2.....Zn 其中 X、Y 是所有曲面共有的坐标向量,Z1、Z2、...、Zn 是曲面。 我想在 Matlab 中导入该文件并将每个单独的曲面保存到其自己的文件 XYZ.

假设数据被导入并存储在变量“data”中,这段代码完成了这项工作:

r=size(data,1);
c=size(data,2)-2;
temp(:,1)=data(:,1);
temp(:,2)=data(:,2);
for i = 1:c
    temp(:,3)=data(:,i+2);
    tempname = ['proportional_',num2str(i*5-5),'ms','.txt']; % naming reflects the 
                                                           % fact that surfaces
                                                           % are in two way travel
                                                           % time and that median 
                                                           % distance between each
                                                           % two surfaces is ~ 5 
                                                           % milliseconds
    save (tempname,'temp','-ASCII','-TABS'); 
end                                                                                                                                                                   
clear r c temp tempname i
clc

我想通过从 for 循环中取出尽可能多的代码来向量化这段代码。我认为为 save 命令执行此操作可能是不可能的,但也许其他两行可以。我的感觉是我应该使用元胞数组或者结构,但我想不出正确的语法。 有什么想法吗?谢谢

I have an ASCII file containing a number of surfaces created in a seismic interpretation software. They are a family of interpolated surfaces between a top surface and a bottom surface are a family All surfaces are sampled at each point of a regular squuare grid.
The file structure is:
X Y Z1 Z2.....Zn
Where X, Y are coordinate vectors common to all surfaces, Z1, Z2,...,Zn are the surfaces.
I would like to import the file in Matlab and save each individual surface to its own file XYZ.

Assuming the data is imported and stored in variable "data", this code does the job:

r=size(data,1);
c=size(data,2)-2;
temp(:,1)=data(:,1);
temp(:,2)=data(:,2);
for i = 1:c
    temp(:,3)=data(:,i+2);
    tempname = ['proportional_',num2str(i*5-5),'ms','.txt']; % naming reflects the 
                                                           % fact that surfaces
                                                           % are in two way travel
                                                           % time and that median 
                                                           % distance between each
                                                           % two surfaces is ~ 5 
                                                           % milliseconds
    save (tempname,'temp','-ASCII','-TABS'); 
end                                                                                                                                                                   
clear r c temp tempname i
clc

I would like to vectorize this code by taking as much of it as possible out of the for loop. I think that doing it for the save command may not be possible, but perhaps the other 2 lines can. My feeling is I should use cell arrays or perhaps structures, but I can't think of the right syntax.
Any ideas? Thank you

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

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

发布评论

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

评论(1

混吃等死 2025-01-14 23:24:07

您可以执行以下操作:

temp = arrayfun( @(x) data(:,[1 2 x]), 3:size(data,2), 'UniformOutput',0 );
arrayfun( @(x) dlmwrite( strcat('proportional_',num2str(x*5-5),'ms.txt'), ...
         temp{x}, '\t' ), 1:numel(temp) );

Here is what you can do:

temp = arrayfun( @(x) data(:,[1 2 x]), 3:size(data,2), 'UniformOutput',0 );
arrayfun( @(x) dlmwrite( strcat('proportional_',num2str(x*5-5),'ms.txt'), ...
         temp{x}, '\t' ), 1:numel(temp) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文