I have a dimension problem by using mesh-plot.The following example works good but I want to plot mesh(zz,TT,u(:,:,2,1)) instead of mesh(u(:,:,2,1)). At this case, the dimensions do not agree and matlab gives error. How can I order this dimension problem?
clear;
z=linspace(0,10,5);
T=linspace(0,20,50);
for j=1:length(T)-1
for i=1:length(z)
u(i,j,2,1)=z(i)*T(j)+10;
end
end
figure(1)
[zz,TT]=meshgrid(z,T);
mesh(u(:,:,2,1))
The code can be simplified as:
z = linspace(0,10,5);
T = linspace(0,20,50);
[zz,TT] = ndgrid(z, T(1:end-1));
uu = zz.*TT + 10;
%u(:,:,2,1) = uu;
mesh(zz, TT, uu)
I take out one less element from T, because thats how you filled the matrix u. Also note the difference between MESHGRID and NDGRID
Your question is not clear at all. Is this what you're looking for?
z=linspace(0,10,5);
T=linspace(0,20,50);
for j=1:length(T)
for i=1:length(z)
u(i,j)=z(i)*T(j)+10;
end
end
[TT, zz]=meshgrid(T, z);
Related
let us suppose we have following code
function plot_test(x,y)
x_constucted=[ones(size(x)) x];
b = regress(y,x_constucted);
y_predicted=b(1)+b(2)*x;
scatter(x,y);
hold on
plot(x,y_predicted);
theString = sprintf('y = %.3f*x+%.3f ', b(2), b(1));
text(x(1), y_predicted(1), theString, 'FontSize', 8);
end
output of this equation is the following figure
my question is : how to align equation out of line? for instance on top left size? thanks in advance
If I understand you correctly, you want to move the printed equation out of the dots. Check out the text() function description. The first two values define the x and y position in your plot for the text.
x=1;
y=25;
To move it up, use the new variables in text(x,y,...). Hope that helps.
Some time ago I was looking for a solution for the same exact problem. As you may know, the legend command allows to specify a Location parameter and one of its many options is called best, described in the official Matlab documentation (here) as follows:
Inside axes where least conflict occurs with plot data
My workaround abuses this feature in order to find the best location to place a single text annotation inside the plot. The code below uses a build-in dataset since you didn't specify how your data looks like:
load carsmall;
x = [ones(size(Horsepower)) Horsepower];
y = MPG;
b = regress(y,x);
y_hat = b(1) + b(2) .* Horsepower;
scatter(Horsepower,y);
hold on;
plot(Horsepower,y_hat);
text_at_best(sprintf('y = %.3f*x+%.3f ',b(2),b(1)),'FontSize',12);
function h = text_at_best(txt,varargin)
l = legend(txt,[varargin{:}]);
t = annotation('textbox',varargin{:});
t.String = txt;
t.Position = l.Position;
t.LineStyle = 'None';
delete(l);
if nargout
h = t;
end
end
Here is the final result:
I don't know if this can fit your needs... but developing an algorithm for finding a non overlapping part of the plot in which to place a text looked like an overkill to me. Despite the text being quite far from the prediction line, it's still elegant, clear and comprehensible. The same goes with an even quicker workaround which consists in setting the regression equation as the plot title (blink blink).
folks. For my college project, I need to plot a half-wave rectifier with the sum of two sine waves. Thus, I have chosen MATLAB to use as a tool, but I am having this problem (after the code):
l=[0:10^-6:1/1500];
sig=8*sin(2*pi*100000*l)+6*sin(2*pi*10000*l);
subplot(211)
plot(sig);
for t=1:667
if (8.*sin(2.*pi.*100000.*l)+6.*sin(2.*pi.*10000.*l))<=0
sig(t)=0;
else
sig(t) = 2.*sin((2.*pi.*100000*l + 2.*pi.*10000*l)/2).*cos(2.*pi.*100000*l - 2.*pi.*10000*l);
end
end
The problem showed on the command screen is: "In an assignment A(:) = B, the number of elements in A and B must be the same". How do I solve this issue?
To obtain the rectified signal there are several forms, but the simplest and most compact way is to use the matrices, in this case it is the following:
l=[0:10^-6:1/1500];
sig = 8*sin(2*pi*100000*l)+6*sin(2*pi*10000*l);
sig_rect = sig.*(sig >= 0);
subplot(211)
plot(sig)
subplot(212)
plot(sig_rect)
If you want to use loops you must do the following:
sig_rect = zeros(length(sig));
for t=1:sig
if sig(t) <=0
sig_rect(t) = 0;
else
sig_rect(t) = sig(t);
end
end
I'm trying to use the meshgrid "function" in Matlab to generate the value of the characteristic function phi of a bivariate normal distribution. Currently I am using two-for loops to do this.
for i = 1:M
for j = 1:M
U = [u1(i); u2(j)];
phi(i,j) = exp(-1i*mu*U-0.5*U'*SIGMA*U);
end
end
Here u1 and u2 are the two vectors spanning the space for which values phi can take. However, this double for-loop is very time consuming, and I would like to do this using a meshgrid approach.
U = meshgrid(u1,u2);
PHI = exp(-1i*mu*[U1(:) U2(:)]-0.5*[U1(:) U2(:)]'*SIGMA.*[U1(:) U2(:)]);
I am trying to do this using the code above. But this is definitely not working due to the different dimensions of mu and [U1(:) U2(:)]. Do anyone have any hints on how this can be done?
This should do the trick:
[U1, U2] = meshgrid(u1,u2);
U = [U1(:) U2(:)]';
PHI = reshape(exp(-1i*mu*U-0.5*sum(U.*(SIGMA*U),1)),M,M).';
PS: Using ndgrid seems to be preferable to meshgrid also in this situation, because we don't need to transpose afterwards.
[U1, U2] = ndgrid(u1,u2);
U = [U1(:) U2(:)]';
PHI = reshape(exp(-1i*mu*U-0.5*sum(U.*(SIGMA*U),1)),M,M);
Solved it by just writing out the matrix multiplications:
[U1,U2]= meshgrid(u1,u2);
PHI = exp(-1i.*(mu_adj(1)*U2+mu_adj(2)*U1)...
-0.5*(SIGMA(1,1)*U2.^2+(SIGMA(2,1)+SIGMA(1,2))*U1.*U2+SIGMA(2,2)*U1.^2));
I have a couple of arrays:
x = [0:pi/16:2*pi]
y = [0:pi/16:2*pi]
And I want to make a matrix xy in this way:
xY = [(0,0) (0,pi/16) ... (0,2pi);
(pi/16,0) (pi/16,pi/16) ... (pi/16,2pi);
: : :
(2pi,0) (2pi,pi/16) ... (2pi,2pi)]
I've tried many things like this:
for i=1:length(x)
for j=1:length(y)
xy{i,j} = [{x(i),y(j)}];
end
end
, but I have encountered many errors.
I know it should be easy, and the biggest problem is that the title of my post (and because of that, the way I'm looking for help) is wrong, so I apologize for that.
I think I should mention that I'm trying to create a multi-layer perceptron that will get trained with that matrices and this formula:
fxy = cos(x)-3*sin(y);
Thanks in advance!
This is exactly what meshgrid is designed for.
The simplest way is to this is to create matrix of size length(x)-by-length(y)-by-2:
A = zeros(length(x), length(y), 2);
for i = 1 : length(x); for j = 1 : length(y); A(i, j, :) = [x(i), y(j)]; end; end;
In your case, matrix A will have size 33x33x2. To get pair using indexes i, j use the following code:
squeeze(A(i, j, :))
Or you can adjust your code to work with such 3-dimensional matrix.
I want to built a "travelling grid" MATLAB. Actually, I had to choose another MATLAB command instead of linspace to built my grid for any k. Is it possible with a MATLAB command?
for k=1:5
a=0;
b(k)=k.*3;
x=linspace(0,b(k),10);
y=linspace(0,30,10);
for z=1:length(x)
for t=1:length(y)
A(z,t,k)=x(z).*exp(-y(t));
end
end
end
Thanks for any help,
X = linspace(0,3,10);
XX(1,:,:) = bsxfun(#times,X,(1:5)')';
Y = exp(-linspace(0,30,10));
B = bsxfun(#times,Y',XX);
B = permute(B,[2,1,3]);
Your current code is working fine, so I'm not sure what the question is... Here is a slightly simpler implementation:
b = (1:5).*3;
A = zeros(10,10,5);
for k=1:5
[X,Y] = ndgrid(linspace(0,b(k),10), linspace(0,30,10));
A(:,:,k) = X.*exp(-Y);
end
If you also want the y-limits to change as well, the process is similar; you would have two loops and the result A being a 4D matrix