I have a list of vectors, like this:
{x = 7, y = 0.}, {x = 2.5, y = 0.}, {x = -2.3, y = 0.}, {x = 2.5, y = 2.7}, {x = 2.5, y = -2.7}
How do I convert these to data I can plot? I've been trying with the "convert" function, but can't get it to work.
When I manually convert it to something like [[7, 0], [2.5, 0], [-2.3, 0], [2.5, 2.7], [2.5, -2.7]] it works, though there has to be an automatic way, right?
A little more info about what I'm doing if you're interested:
I have a function U(x,y), of which I calculate the gradient and then check where it becomes 0, like this:
solve(convert(Gradient(U(x, y), [x, y]), set), {x, y});
that gives me my list of points. Now I would like to plot these points on a graph.
Thanks!
S:={x = 7, y = 0.}, {x = 2.5, y = 0.}, {x = -2.3, y = 0.},
{x = 2.5, y = 2.7}, {x = 2.5, y = -2.7}:
T:=map2(eval,[x,y],[S]);
[[7, 0.], [2.5, 0.], [-2.3, 0.], [2.5, 2.7], [2.5, -2.7]]
Related
Within a neural network, I have some 2D feature maps with values between 0 and 1. For these maps, I want to calculate the covariance matrix based on the values at each coordination. Unfortunately, pytorch has no .cov() function like in numpy. So I wrote the following function instead:
def get_covariance(tensor):
bn, nk, w, h = tensor.shape
tensor_reshape = tensor.reshape(bn, nk, 2, -1)
x = tensor_reshape[:, :, 0, :]
y = tensor_reshape[:, :, 1, :]
mean_x = torch.mean(x, dim=2).unsqueeze(-1)
mean_y = torch.mean(y, dim=2).unsqueeze(-1)
xx = torch.sum((x - mean_x) * (x - mean_x), dim=2).unsqueeze(-1) / (h * w - 1)
xy = torch.sum((x - mean_x) * (y - mean_y), dim=2).unsqueeze(-1) / (h * w - 1)
yx = xy
yy = torch.sum((y - mean_y) * (y - mean_y), dim=2).unsqueeze(-1) / (h * w - 1)
cov = torch.cat((xx, xy, yx, yy), dim=2)
cov = cov.reshape(bn, nk, 2, 2)
return cov
Is that the correct way to do it?
Edit:
Here is a comparison with the numpy function:
a = torch.randn(1, 1, 64, 64)
a_numpy = a.reshape(1, 1, 2, -1).numpy()
torch_cov = get_covariance(a)
numpy_cov = np.cov(a_numpy[0][0])
torch_cov
tensor([[[[ 0.4964, -0.0053],
[-0.0053, 0.4926]]]])
numpy_cov
array([[ 0.99295635, -0.01069122],
[-0.01069122, 0.98539236]])
Apparently, my values are too small by a factor of 2. Why could that be?
Edit2: Ahhh I figured it out. It has to be divided by (h*w/2 - 1) :) Then the values match.
The quiz I met first gave me an Logistic model:
And ask me to linearize it, then evaluate the value of a and k according to the data it gave( in this subject L is took as 3000). I finished that, but got into trouble in the second subject which asked me to do a non-linear-regression with a and k's value evaluated in the first subject. Here's my code:
function y = func(const, t)
y = const(1)./(1 + const(2)*exp(-const(3)*t));
end
t = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
y = [43.65, 109.86, 187.21, 312.67, 496.58, 707.65 , ...
960.25, 1238.75, 1560, 1824.29, 2199, 2438.89, 2737.71];
yAss = log ((3000 ./ y) - 1);
p = polyfit (t, yAss, 1);
a = exp (1) ^ (p(2));
k = -p(1);
beta0 = [3000, a, k];
beta = nlinfit (t, yAss, #func, beta0);
yAfter = beta(1) ./ (1 + beta(2) * exp (-beta(3) * t));
yCompare = 3000 ./ (1 + a * exp (-k * t));
scatter (t, y); hold on;
plot (t, yAfter, 'r');
plot (t, yCompare);
And what it gave:
The red curve is generated with the value returned by nlinfit, Anyone could tell me what is wrong?
I feel stupid... Answering my own question and the question itself is stupid either...
beta = nlinfit (t, yAss, #func, beta0);
should be:
beta = nlinfit (t, y, #func, beta0);
I really want to delete that question...
I don't understand how [0:1:5] is being used in the code below:
function [x , y] = plotTrajectory(Vo,O,t,g)
% calculating x and y values
x = Vo * cos(O) * t ;
y = Vo*(sin(O)*t)-(0.5*g*(t.^2));
plot (x,y);
hold on
end
for i = (0: (pi/8): pi);
[x,y] = plotTrajectory(10,i,[0:1:5],9.8);
end
Each of the parameters are being used to find particular X and Y values. O changes from 0 to pi in steps of pi/8 while Vo, t and g remain unchanged.
The t variable is simply an array from 0 to 5 in steps of 1 and so there are 6 time points defined all together. With these time points and with a particular value of O, but with the values of Vo, t and g being held constant throughout this entire endeavour, 6 X and Y points are defined and are thus plotted on a graph. A graph is generated for each value of O and thus a set of 6 different X and Y points are generated. Each graph with each value of O are all plotted on the same graph.
We can rewrite the above code in pseudo-code to make it easier to understand as follows:
for i = 0, pi/8, 2*pi/8, ..., pi
define Vo = 10
define O = i
define t = [0, 1, 2, 3, 4, 5]
define g = 9.8
run function plotTrajectory(Vo, O, t, g)
end
function plotTrajectory(Vo, O, t, g)
calculate x = Vo * cos(O) * t, for t = 0, 1, 2, 3, 4, 5
calculate y = Vo * (sin(O) * t) - (0.5 * g * t^2), for t = 0, 1, 2, 3, 4, 5
plot x and y for t = 0, 1, 2, 3, 4, 5 on the same graph
end
I have a problem where I have two choice variables x1 and x2 which then pin down a third x3 = 1 - x1 - x2. I would like to loop through various values of [x1, x2, x3]. This code works:
w1 = perms([0.1, 0.1, 0.8]);
w2 = perms([0.1, 0.2, 0.7]);
w3 = perms([0.1, 0.3, 0.6]);
w4 = perms([0.1, 0.4, 0.5]);
w5 = perms([0.2, 0.2, 0.6]);
w6 = perms([0.2, 0.3, 0.5]);
w7 = perms([0.2, 0.4, 0.4]);
w8 = perms([0.3, 0.3, 0.4]);
w = [w1; w2; w3; w4; w5; w6; w7; w8];
w = unique(w,'rows');
% loop
for ii = 1:size(w, 1)
... do some stuff with w(ii, :)
but I am wondering if there is a more elegant way to do this.
This is a classical case for ndgrid
[x1,x2]=ndgrid(0.1:0.1:0.8,0.1:0.1:0.8);
x3 = 1-x1-x2;
%I assume from your example that we want x1,x2,x3 in (0,1) OPEN interval, then:
valid_points = x3>0 & x3 <1
w_prime = [x1(valid_points),x2(valid_points),x3(valid_points)];
I want to organize the data returned by sph2cart as a matrix with vector elements, and operate on each element in this matrix (vector-vector or vector-scalar calculation). Here is an example wher I achieve this:
lightV = zeros(1, 1, 3);
lightV(1,1,1) = 0.5;
lightV(1,1,2) = 0.4;
lightV(1,1,3) = 0.7;
[Az El] = meshgrid(0:60:360, 0:15:90);
[x y z] = sph2cart(Az*pi/180, El*pi/180, 1);
refV = zeros(size(Az,1), size(Az,2), 3);
radius = zeros(size(Az,1), size(Az,2));
for i = 1:size(Az,1)
for j = 1:size(Az,2)
refV(i,j,1) = -x(i,j);
refV(i,j,2) = -y(i,j);
refV(i,j,3) = z(i,j);
radius(i,j) = dot(refV(i,j,:), lightV(1,1,:));
end
end
However this looks somewhat redundant, how could I make it more terse?
Well, you can vectorize your code like so:
S.lightV = [0.5, 0.4, 0.7];
[Az, El] = meshgrid(0:60:360, 0:15:90);
[S.x, S.y, S.z] = sph2cart(Az * pi/180, El * pi/180, 1);
S.refV = cat(3, -x, -y, z);
S.radius = sum(bsxfun(#times, S.refV, reshape(S.lightV, 1, 1, [])), 3);
Note the usage of cat to concatenate along the third dimension, and the combination of bsxfun and sum to replace the dot product inside the nested for loop. I've also bound everything together in one struct S.