Create a contour plot from 3 vectors - matlab

I'm trying to create a contour plot from this data:
pH D Tur
5.10 3 79.18918919
5.50 6 92.97297297
5.00 0 50.09009009
5.00 6 90.36036036
5.10 9 91.08108108
5.10 12 89.18918919
5.10 15 83.6036036
5.00 18 91.26126126
5.00 21 81.26126126
5.00 24 90.99099099
5.00 27 91.44144144
5.00 30 90.45045045
6.00 0 43.42342342
5.64 3 81.8018018
5.50 9 92.16216216
5.50 0 44.68468468
5.40 12 92.34234234
5.50 15 92.25225225
5.50 18 91.62162162
5.50 21 90.81081081
5.50 24 91.8018018
5.50 27 92.52252252
5.50 30 90.36036036
6.10 3 81.98198198
6.00 6 93.51351351
6.00 9 94.77477477
6.10 12 95.04504505
6.00 15 94.68468468
5.90 18 94.05405405
6.00 21 94.68468468
6.00 24 94.41441441
6.00 27 93.69369369
6.00 30 94.5045045
6.50 0 41.08108108
6.50 3 76.03603604
6.50 6 87.92792793
6.60 9 94.32432432
6.50 12 94.77477477
6.50 15 94.32432432
6.50 18 94.95495495
6.50 21 94.41441441
6.40 24 93.33333333
6.40 27 94.41441441
6.40 30 94.14414414
7.00 0 41.17117117
7.00 3 61.71171171
6.90 6 84.05405405
6.90 9 89.72972973
6.90 12 90.81081081
6.90 15 91.53153153
6.90 18 91.44144144
6.86 21 91.53153153
6.86 24 91.98198198
6.86 27 90.81081081
6.90 30 92.79279279
7.44 3 65.85585586
7.50 6 79.72972973
7.50 0 59.00900901
7.50 9 81.35135135
7.50 12 79.00900901
7.50 15 81.98198198
7.50 18 83.69369369
7.50 21 81.17117117
7.50 24 80.09009009
7.30 27 89.63963964
7.50 30 81.98198198
I've imported the data as 3 different vectors: pH, D, and Tur
I created a mesh and griddata and plot the contour
[X Y]=meshgrid(pH,D);
Z=griddata(pH,D,Tur,X,Y);
contourf(X,Y,Z)
I was hoping something like that:
But I'm getting this:

You need to sort your inputs to meshgrid first:
[X, Y] = meshgrid(sort(pH), sort(D));
Z = griddata(pH, D, Tur, X, Y);
contourf(X, Y, Z);
Or another alternative is to use unique to both sort and remove redundant values from pH and D, reducing the size of the matrices produced by meshgrid:
[X, Y] = meshgrid(unique(pH), unique(D));
Z = griddata(pH, D, Tur, X, Y);
contourf(X, Y, Z);
And both of the above options give the same graphical result:

Related

Wcnf dimacs how to get the value with highest weight

Let's say i have this problem and wanted to solve it using dimacs and maxsat solvers
There's 10 police patrols and i want solver to pick the best police patrol to go to intervention, each patrol is described by 3 variables (status, distance, districts)
so there will be 3 group of clauses
for example first patrol will be PP1 = x1,x11,x21, PP2 = x2,x12,x22 PP3 = x3,x13,x23 .. PP10 = x10,x20,x30
group 1 describing police patrol status, (300 means weight)
300 C1 - (x1 v x2 v x3)
50 C2 - (x4 v x5)
10 C3 - (x6 v x7 v x8 v x9 v x10)
C1 means their status is the best and C3 means it's the worst
group 2 describing police patrol distance to some incident or crime happening
300 C4 - (x11 v x12 v x13)
50 C5 - (x14 v x15)
10 C6 - (x16 v x17 v x18 v x19 v x20 )
C4 means they are the closest to incident, in C6 they are farthest
group 3 describing in what district they are
300 C7 - (x21 v x22 v x23)
50 C8 - (x24 v x25)
10 C9 - (x26 v x27 v x28 v x29 v x30)
C7 will be the safest etc.
So this is my wcnf file in dimacs, i don't know if it's good but will be pleased if you correct me what's wrong with it
p wcnf 30 9
300 1 2 3 0
50 4 5 0
10 6 7 8 9 10 0
300 20 11 14 0
50 15 16 17 0
10 12 13 18 19 0
300 29 21 27 0
50 22 23 24 25 0
10 26 28 30 0
I tested it in 2 solvers, rc2 maxsat solver and EvalMaxSAT and output was like this:
EvalMaxSAT
s OPTIMUM FOUND
o 0
v -1 2 -3 4 -5 6 -7 -8 -9 -10 11 12 -13 -14 15 -16 -17 -18 -19 -20 21 22 -23 -24 -25 26 -27 -28 -29 -30
c Total time: 335 µs
-
rc2
c formula: 30 vars, 0 hard, 9 soft
s OPTIMUM FOUND
o 0
v 1 -2 -3 4 -5 6 -7 -8 -9 -10 11 12 -13 -14 15 -16 -17 -18 -19 -20 21 22 -23 -24 -25 26 -27 -28 -29 -30
but looking at my wcnf file, I think ideal output should get values 1,11,21 as true because they are in the clauses with highest weight

Convert hourly data to daily data in Matlab

We have two matrices. Name one of them "Date", And another name is "Data"
There are several columns in the Date matrix included:
year month day julusi hour
1951 1 1 1 0
1951 1 1 1 3
1951 1 1 1 6
1951 1 1 1 9
1951 1 1 1 12
1951 1 1 1 15
1951 1 1 1 18
1951 1 1 1 21
1951 1 2 2 0
1951 1 2 2 3
1951 1 2 2 6
1951 1 2 2 9
1951 1 2 2 12
1951 1 2 2 15
1951 1 2 2 18
1951 1 2 2 21
.... . . . .
.... . . . .
1951 12 30 364 0
1951 12 30 364 3
1951 12 30 364 6
1951 12 30 364 9
1951 12 30 364 12
1951 12 30 364 15
1951 12 30 364 18
1951 12 30 364 21
1951 12 31 365 0
1951 12 31 365 3
1951 12 31 365 6
1951 12 31 365 9
1951 12 31 365 12
1951 12 31 365 15
1951 12 31 365 18
1951 12 31 365 21
.... .. . .. .
2018 12 31 365 0
2018 12 31 365 3
2018 12 31 365 6
2018 12 31 365 9
2018 12 31 365 12
2018 12 31 365 15
2018 12 31 365 18
2018 12 31 365 21
In my Data matrix, there are 410 columns(198696*410).The size of my Date matrices is equal. "198696*1". I want to convert the "Data Matrix on basis the Date Matrix to daily data
I use the following code
N=0;
for year=1951:2018;
for Juliusi=1:365;
cxa=(Date(:,4)==Juliusi);
cxb=(Date(:,1)==year);
a=cxa & cxb;
N=N+1;
dayy(N,:)=nanmean(Data(a,:));
end;end;
The conversion result is correct, but the size of the matrix is not the same
198696/8=24837 is correct but my matrix 24820 is incorrect
Where is the problem?
What to do to consider leap days?
Since I recently learned from Luis Mendo, that convolution is the key to success, I came up with the following idea: If your data is complete, i.e. you can guarantee, that there are always 8 entries for each day, you can just simply use the following approach:
% Some test data.
Date = [
1951 1 1 1 0;
1951 1 1 1 3;
1951 1 1 1 6;
1951 1 1 1 9;
1951 1 1 1 12;
1951 1 1 1 15;
1951 1 1 1 18;
1951 1 1 1 21;
1952 1 2 2 0;
1952 1 2 2 3;
1952 1 2 2 6;
1952 1 2 2 9;
1952 1 2 2 12;
1952 1 2 2 15;
1952 1 2 2 18;
1952 1 2 2 21]
% Temporary result for convolution.
temp = conv2(Date, ones(8, 1)) / 8;
% Extract values of interest.
dayy = temp(8:8:end, :)
Output:
Date =
1951 1 1 1 0
1951 1 1 1 3
1951 1 1 1 6
1951 1 1 1 9
1951 1 1 1 12
1951 1 1 1 15
1951 1 1 1 18
1951 1 1 1 21
1952 1 2 2 0
1952 1 2 2 3
1952 1 2 2 6
1952 1 2 2 9
1952 1 2 2 12
1952 1 2 2 15
1952 1 2 2 18
1952 1 2 2 21
dayy =
1951.0000 1.0000 1.0000 1.0000 10.5000
1952.0000 1.0000 2.0000 2.0000 10.5000
If you need the year and day information, then these could be obtained separately. But in your original post, these information seemed to be unneeded.
Just to be sure: I DO know, I used the Date matrix in my example. But since, Date follows the same format as Data, and you can easily verify the results of the wanted mean operation, I used it as an example.

How to calculate intensity inhomogeneity based on average filter by matlab

I have a question about intensity inhomogeneity. I read a paper, it defined a way to calculate the intensity inhomogeneity based on average filter:
Let see my problem, I have a image I (below code) and a average filter with r=3. I want to calculate image transformation J based on formula (17). Could you help me to implement it by matlab code? Thank you so much.
This is my code
%Create image I
I=[3 5 5 2 0 0 6 13 1
0 3 7 5 0 0 2 8 6
4 5 5 4 2 1 3 5 9
17 10 3 1 3 7 9 9 0
7 25 0 0 5 0 10 13 2
111 105 25 19 13 11 11 8 0
103 105 15 26 0 12 2 6 0
234 238 144 140 51 44 7 8 8
231 227 150 146 43 50 8 16 9
];
%% Create filter AF
size=3; % scale parameter in Average kernel
AF=fspecial('average',[size,size]); % Average kernel
%%How to calculate CN and J
CN=mean(I(:));%Correct?
J=???
You're pretty close! The mean intensity is calculated correctly; all you are missing to calculate J is apply the filter defined with fspecial to your image:
Here is the code:
clc
clear
%Create image I
I=[3 5 5 2 0 0 6 13 1
0 3 7 5 0 0 2 8 6
4 5 5 4 2 1 3 5 9
17 10 3 1 3 7 9 9 0
7 25 0 0 5 0 10 13 2
111 105 25 19 13 11 11 8 0
103 105 15 26 0 12 2 6 0
234 238 144 140 51 44 7 8 8
231 227 150 146 43 50 8 16 9
];
% Create filter AF
size=3; % scale parameter in Average kernel
AF=fspecial('average',[size,size]); % Average kernel
%%How to calculate CN and J
CN=mean(I(:)); % This is correct
J = (CN*I)./imfilter(I,AF); % Apply the filter to the image
figure;
subplot(1,2,1)
image(I)
subplot(1,2,2)
image(J)
Resulting in the following:

Aligning face data from MATLAB triangulation

I have a function f(x,y) which has certain symmetries that I would like to plot. Here is an example:
This plot can be generated with:
[x,y,z] =
0 0 0.1415
0.1999 0.1999 0.1165
0.2760 0 0.1268
0.3694 0.3694 0.0983
0.4830 0 0.1142
0.5090 0.5090 0.0903
0.5550 0.1871 0.0881
0.6189 0.3558 0.0715
0.6197 0.6197 0.0907
0.6399 0 0.1056
0.7071 0.7071 0.1415
0.7169 0.4835 0.0869
0.7215 0.1200 0.0859
0.7304 0.2392 0.0680
0.7643 0 0.1005
0.7926 0.3574 0.0856
0.8090 0.5878 0.1393
0.8581 0.1122 0.0821
0.8634 0.2343 0.0878
0.8794 0 0.0986
0.8910 0.4540 0.1332
0.9511 0.3090 0.1253
0.9877 0.1564 0.1191
1.0000 0 0.1169
t =
6 4 8
12 6 8
8 4 7
4 2 7
8 7 14
14 7 13
3 2 1
5 7 3
3 7 2
17 12 21
6 12 9
9 17 11
12 17 9
10 15 13
10 7 5
13 7 10
21 12 16
16 12 8
8 14 16
18 14 13
15 20 18
13 15 18
24 23 18
18 20 24
21 16 22
23 22 19
19 18 23
14 18 19
19 16 14
19 22 16
trisurf(t,x,y,z)
So I know that function has a reflection symmetry about y=x and then the resulting function is to be repeated in all the quadrants. Here is the code to do this:
allx = [x; x;-x;-x;y; y;-y;-y];
ally = [y;-y; y;-y;x;-x; x;-x];
allz = [z; z; z; z;z; z; z; z];
These are the new vertices for the surface I want to plot. Now how do I properly generate the faces for this new surface?
When I use a finer mesh and add some pretty lights it should look something like this:
Speculative:
So your question is about how to set-up the first argument of trisurf, i.e. how to define the extended t in your code. According to the docs this is the index into the vertices defined by the remaining arguments. I don't have MATLAB installed on this machine, but what happens if you do:
allx = [x; x;-x;-x];
ally = [y;-y; y;-y];
allz = [z; z; z; z];
s = size(x,1);
t = [t; t + s; t + 2*s; t + 3*s]
Just trying to think if this makes sense and if/how it extends into the other quadrants.

How can I visualize/plot temperature gradient using matlab?

I have data at specific points in 3D rectangle and I want to see temperature gradient. I have values at specific points , but I want a continous flow of gradient between each sensor. I am not been able to figure out how to visualize or map data in between each sensors placed at different points. stucked :(
X=[5 0 0 0 0 5 10 10 10 10 0 5 10 10 0 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 0 5 10 0 5 10 10 10 5 0 0]';
Y=[10 10 5 5 10 10 5 10 5 10 0 0 0 0 0 0 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 3.5 7 0 0 0 0 0 0 5 10 10 10 5 ]';
Z=[20 20 20 14 14 14 14 14 20 20 20 20 20 14 14 14 3.8 3.8 0 0 7.5 7.5 10 10 12.5 12.5 15 15 17.5 17.5 20 20 0 0 0 7.5 7.5 7.5 7.5 7.5 7.5 7.5 7.5]';
%# temperature vector
T = [20 22 24 22.1 26.1 22.4 15 17 21 22 19 22 18 17 18 20 21 22 21 24 22.3 22.5 22.8 28.9 22 27 26 20 19 24 21 23 19 18 22 25 27 21 29 25 22 21 22];
scatter3(X,Y,Z,[4000],T,'.');
grid off
box off
view(32,18); axis equal tight off vis3d; % azimuth 26
camproj perspective
camlight; lighting gouraud; alpha(0.75);
rotate3d on
Code below just shows how my one side of 3d rectangle should look like(its just a random code)
datagrid = 500*peaks(100);
R = makerefmat('RasterSize',size(datagrid));
[aspect,slope,gradN,gradE] = gradientm(datagrid,R);
figure; axesm eqacyl
meshm(datagrid,R)
colormap (jet(64))
colorbar('vert')
title('Peaks: elevation')
axis square
You can break the problem down into two sub-problems:
Interpolation
Visualization
Let's take a look at interpolation first. There are many methods available but let's try the MATLAB function griddatan. This will interpolate (linearly) values onto a new set of points (here I've used a regular grid constructed using meshgrid).
M = 20;
N = 20;
L = 40;
T = transpose(T);
% Set up the grid of points we wish to interpolate at
[xi,yi,zi] = meshgrid(linspace(0,10,M),linspace(0,10,N),linspace(0,20,L));
% Perform interpolation
ti = griddatan([X,Y,Z],T,[xi(:),yi(:),zi(:)]);
% Reshape back from a column vector to a MxNxL matrix
ti = reshape(ti, size(xi));
% Visualise in some way
scatter3(xi(:),yi(:),zi(:),400,ti(:),'.')
When it comes to visualization then the sky's the limit and 3D volume visualization is more of an art than a science. I'm afraid I can't run your example (I don't have access to makerefmat) but http://www.mathworks.co.uk/help/techdoc/visualize/bqliccy.html has some good starting points.