Reputation: 2347
One afternoon wasted on the following problem:
I have a matrix of depth values in a certain spatial domain. I define a line in the domain and I erase the valued below this line.
The code uses the findnearest
function to find the index of the element of an array that is closest to a certain value.
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')
The resulting depth matrix is the following:
that represented with contour
looks like this:
I want to rotate the depth matrix by an angle (-pi/2-a
?) or apply some transformation to it, such that the depth contours would become perpendicular to a line parallel to the first line:
I simply various rotation matrices but with no good results...
Upvotes: 4
Views: 446
Reputation: 541
I do not have a full answer, but i do found a solution to this problem with some adaptations.
What i did:
When you have the dividing line, that are the important values. For the points inside the area that is rotated, I found the value going in the x direction and copying it from the original depth matrix. To make it easy, i made the values on x data have the same distance as y.
And after these adaptations i got this:
Note that i also made the notations for what i did as well.
However, in your code, the X/Y ratio is not the same. If i just copy my part of the code into yours code, i get vertical lines, as your angle is ~0, despite in the image is close to 45 degrees. To made the proper adjust, you need to actually make this comparison by values rather than indexes, as i have done.
And here goes the adapted code (for comparison, look for the %ADDED comment):
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')
Upvotes: 1