Rotate the matrix a certain angle

Once wasted on the following problem:

I have a matrix of depth values ​​in a specific spatial area. I am defining a string in a domain and I am deleting a value below this string.

The code uses a function findnearestto find the index of an array element that is closest to a specific 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 ​​as follows:

depth

represented with help contourlooks like this:

depth_c

I want to rotate the depth matrix by an angle ( -pi/2-a?) Or apply some transformation to it, so that the depth contours become perpendicular to the line parallel to the first row:

idea

I'm just different rotation matrices, but without good results ...

+4
1

, .

:

, . , x, . , , .

: 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')
+1

Source: https://habr.com/ru/post/1682037/


All Articles