Looping of grid plot
Show older comments
Hello,
with the aid of @Mathieu NOE and @Bjorn Gustavsson I was able to create a countour plot and a Nx x Ny grid partition.
The average of every "grid cell" is calculated and ploted in the first figure.
Subsequent a second plot (figure(2)) is created which consists of plain Nx x Ny grid fields.
The code is shown below:
clc,close all
clearvars
N = 100; % <= added
Lx=2;
Ly=2;
x = linspace(0,Lx,N);
y = linspace(0,Ly,N);
[X,Y] = meshgrid(x,y);
Z = sin(X) + cos(Y);
figure(1)
[C,h] = contourf(X,Y,Z,10);
hold on
% let(s do a 4 x 4 area division and compute the individual cells average
Nx = 4;
Ny = 4;
dx = floor(N/Nx);
dy = floor(N/Ny);
figure(1)
for ci = 1:Nx
for cj = 1:Ny
indX = (1:dx)+(ci-1)*dx;
indY = (1:dy)+(cj-1)*dy;
% centroid of cell
xc = mean(x(indX));
yc = mean(y(indY));
% mean of submatrix
Zmean(ci,cj) = mean(Z(indX,indY),'all');
plot(xc,yc,'*r','Markersize',15);
t = text(xc+0.15,yc+0.15,num2str(Zmean(ci,cj),3),'FontSize',14,'Color','r');
end
end
M=rot90(Zmean);
dx=Lx/Nx;
dy=Ly/Ny;
% Vertices of rectangle
%column 1
R1 = [0 0;0*dx 1*dy;1*dx 1*dy;1*dx 0*dy];
R2 = [0*dx 1*dy;0*dx 2*dy;1*dx 2*dy;1*dx 1*dy];
R3 = [0*dx 2*dy;0*dx 3*dy;1*dx 3*dy;1*dx 2*dy];
R4 = [0*dx 3*dy;0*dx 4*dy;1*dx 4*dy;1*dx 3*dy];
%column 2
R5 = [1*dx 0;1*dx 1*dy;2*dx 2*dy;2*dx 0*dy];
R6 = [1*dx 1*dy;1*dx 2*dy;2*dx 2*dy;2*dx 1*dy];
R7 = [1*dx 2*dy;1*dx 3*dy;2*dx 3*dy;2*dx 2*dy];
R8 = [1*dx 3*dy;1*dx 4*dy;2*dx 4*dy;2*dx 3*dy];
%column 3
R9 = [2*dx 0;2*dx 1*dy;3*dx 2*dy;3*dx 0*dy];
R10 = [2*dx 1*dy;2*dx 2*dy;3*dx 2*dy;3*dx 1*dy];
R11 = [2*dx 2*dy;2*dx 3*dy;3*dx 3*dy;3*dx 2*dy];
R12 = [2*dx 3*dy;2*dx 4*dy;3*dx 4*dy;3*dx 3*dy];
%Column 4
R13 = [3*dx 0;3*dx 1*dy;4*dx 2*dy;4*dx 0*dy];
R14 = [3*dx 1*dy;3*dx 2*dy;4*dx 2*dy;4*dx 1*dy];
R15 = [3*dx 2*dy;3*dx 3*dy;4*dx 3*dy;4*dx 2*dy];
R16 = [3*dx 3*dy;3*dx 4*dy;4*dx 4*dy;4*dx 3*dy];
figure(2)
patch(R1(:,1),R1(:,2),'r','EdgeColor','k')
hold on
patch(R2(:,1),R2(:,2),'b','EdgeColor','k')
hold on
patch(R3(:,1),R3(:,2),'g','EdgeColor','k')
hold on
patch(R4(:,1),R4(:,2),'y','EdgeColor','k')
hold on
patch(R5(:,1),R5(:,2),'y','EdgeColor','k')
hold on
patch(R6(:,1),R6(:,2),'g','EdgeColor','k')
hold on
patch(R7(:,1),R7(:,2),'b','EdgeColor','k')
hold on
patch(R8(:,1),R8(:,2),'r','EdgeColor','k')
hold on
patch(R9(:,1),R9(:,2),'r','EdgeColor','k')
hold on
patch(R10(:,1),R10(:,2),'b','EdgeColor','k')
hold on
patch(R11(:,1),R11(:,2),'g','EdgeColor','k')
hold on
patch(R12(:,1),R12(:,2),'y','EdgeColor','k')
hold on
patch(R13(:,1),R13(:,2),'y','EdgeColor','k')
hold on
patch(R14(:,1),R14(:,2),'g','EdgeColor','k')
hold on
patch(R15(:,1),R15(:,2),'b','EdgeColor','k')
hold on
patch(R16(:,1),R16(:,2),'r','EdgeColor','k')

I would like to automate the generation of the grid (found in the code under "% Vertices of rectangle") or create it via a loop so that the individual points R1 to RN no longer have to be created manually for a large number.
Likewise, the following illustration should be automated or looped with (found in the code under "figure(2)") so that the "patch(..)" lines no longer have to be created manually for a large number.
Is it possible to assign a specific colour to each cell depending on the value of the mean value of the respective cell /for figure(2))? E.g. at <=0.125 red, 0.125 <mean<=0.5 blue, etc.
Maybe someone has a hind how to achieve this.
Best Regards
Steffen B.
Accepted Answer
More Answers (0)
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

