gpt4 book ai didi

matlab - 将矩阵旋转一定角度

转载 作者:太空宇宙 更新时间:2023-11-03 20:05:47 41 4
gpt4 key购买 nike

一个下午浪费在了以下问题上:

我在某个空间域中有一个深度值矩阵。我在域中定义了一条线,然后删除了这条线下方的值。

代码使用 findnearest函数查找最接近特定值的数组元素的索引。

clear all
close all

dx = 5;
dz = 1;

%angle between line and ground
a=atan(0.05);

%%%define domain
xi = 0:dx:20e3;
zi = 0:dz:1000;
m=length(zi);
n=length(xi);

%create grid
[x,z] = meshgrid(xi,zi);

%z where line starts
zs = 700;
%set line
for i = 1:findnearest(xi,zi(zs)*1/a)
xind(i) = i;
zind(i) = findnearest(zi, fix(-xi(i)*a +zi(zs)));
end


depth = repmat(zi',1,n); %simply the coordinate zi repeated for all xi

%calculate distance from the line
for ii=1:n %for every x

zslope = -a*xi(ii)+zi(zs);%equation of the line

zz(ii)=zslope;
if zslope>=0 %if the line is still in the domain (z>0)
for jj=1:m %for every z

if zi(jj)>=zslope %above the line

Zs(jj,ii) = zi(jj)-zslope; %height above the line

elseif zi(jj)<zslope %below the line (ground)
%
Zs(jj,ii)=NaN;

end
end%for on z

elseif zslope<0 %the line is no longer in the domain

for jj=1:m %for every z

Zs(jj,ii) = zi(jj)-zslope; %height above the line

end
end
end%for on x

figure
imagesc(Zs)
colorbar
title('distance from the line')

%zone above the line
maskINT=zeros(m,n);
inds = find(Zs>=0); %erase values under the line
maskINT(inds)=1;


figure
imagesc(depth);colorbar
title('depth')

figure
imagesc(depth.*maskINT);colorbar
title('depth above the line')

figure
contour(depth.*maskINT);colorbar
set(gca,'YDir','Reverse')
title('depth')

生成的深度矩阵如下:

depth

contour表示的是这样的:

depth_c

我想将深度矩阵旋转一个角度 (-pi/2-a?) 或对其应用一些变换,这样深度等高线就会垂直于平行于第一行:

idea

我简单地使用了各种旋转矩阵,但没有很好的结果......

最佳答案

我没有完整的答案,但我确实通过一些调整找到了解决这个问题的方法。

我做了什么:

当你有分界线时,这就是重要的值(value)观。对于旋转区域内的点,我找到了 x 方向的值并从原始深度矩阵复制它。为了简单起见,我让 x 数据上的值与 y 具有相同的距离。

在这些改编之后我得到了这个: this请注意,我也为我所做的做了标记。

但是,在您的代码中,X/Y 比率并不相同。如果我只是将我的代码部分复制到你的代码中,我会得到垂直线,因为你的角度是 ~0,尽管在图像中接近 45 度。要进行适当的调整,您实际上需要按值而不是索引进行比较,就像我所做的那样。

这里是改编后的代码(为了比较,查找 %ADDED 注释):

clear all
close all

dx = 1; %ADDED
dz = 1;

%angle between line and ground
angle=pi/3; %ADDED
a=atan(angle);

%%%define domain
xi = 0:dx:1000;%ADDED
zi = 0:dz:1000;%ADDED
m=length(zi);
n=length(xi);



%%%ADDED %prealocating
Zs=zeros(m,n);
Zs2=zeros(m,n);
depth2=zeros(m,n);
zz=zeros(1,n);



%create grid
[x,z] = meshgrid(xi,zi);

%z where line starts
zs = 700;
%set line
for i = 1:findnearest(xi,zi(zs)*1/a)
xind(i) = i;
zind(i) = findnearest(zi, fix(-xi(i)*a +zi(zs)));
end


depth = repmat(zi',1,n); %simply the coordinate zi repeated for all xi

%calculate distance from the line
for ii=1:n %for every x

zslope = -a*xi(ii)+zi(zs);%equation of the line

zz(ii)=zslope;
if zslope>=0 %if the line is still in the domain (z>0)
for jj=1:m %for every z

if zi(jj)>=zslope %above the line

Zs(jj,ii) = zi(jj)-zslope; %height above the line

elseif zi(jj)<zslope %below the line (ground)
%
Zs(jj,ii)=NaN;

%ADDED
Zs2(jj,ii)=abs(zi(jj)-zslope);%height above the line
depth2(jj,ii)= depth(jj+round(Zs2(jj,ii)*cos(angle)),ii);

end
end%for on z

elseif zslope<0 %the line is no longer in the domain

for jj=1:m %for every z

Zs(jj,ii) = zi(jj)-zslope; %height above the line

end
end
end%for on x

figure
imagesc(Zs)
colorbar
title('distance from the line')


%ADDED
figure
imagesc(depth2)
colorbar
title('depth2')


%zone above the line
maskINT=zeros(m,n);
inds = find(Zs>=0); %erase values under the line
maskINT(inds)=1;

%ADDED
figure
imagesc(depth2+depth.*maskINT)
colorbar
title('depth summed')


figure
imagesc(depth);colorbar
title('depth')

figure
imagesc(depth.*maskINT);colorbar
title('depth above the line')

figure
contour(depth.*maskINT);colorbar
set(gca,'YDir','Reverse')
title('depth')

关于matlab - 将矩阵旋转一定角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45255438/

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