I'm calculating the eigenvalues of a 8x8-matrix including a symbolic variable "W". Plotting the 8 eigenvalues as functions of W returns a strange result in the plot which looks like someone rode his bike over my diagram.
For the 2nd matrix, where I just set some off-diagonal elements equal to 0, everything works fine. But I don't know what the problem with the 1st one is.
syms W;
w0=1/780;
wl=1/1064;
h=1; % for now this seems unnecessary, but I want to change this value later on
% This is the 1st matrix which causes some strange plotting results
A=h*[w0+3*wl 2*W 0 0 0 sqrt(3)*W 0 0;
2*W 4*wl 0 0 0 0 0 0;
0 0 2*wl+w0 sqrt(3)*W 0 0 0 sqrt(2)*W;
0 0 sqrt(3)*W 3*wl 0 0 0 0;
0 0 0 0 wl+w0 sqrt(2)*W 0 0;
sqrt(3)*W 0 0 0 sqrt(2)*W 2*wl 0 0;
0 0 0 0 0 0 w0 W;
0 0 sqrt(2)*W 0 0 0 W wl];
% This is the 2nd matrix for which everything is working fine
B=h*[w0+3*wl 2*W 0 0 0 0 0 0;
2*W 4*wl 0 0 0 0 0 0;
0 0 2*wl+w0 sqrt(3)*W 0 0 0 0;
0 0 sqrt(3)*W 3*wl 0 0 0 0;
0 0 0 0 wl+w0 sqrt(2)*W 0 0;
0 0 0 0 sqrt(2)*W 2*wl 0 0;
0 0 0 0 0 0 w0 W;
0 0 0 0 0 0 W wl];
X = eig(A);
X2 = eig(B);
eva22 = X2(1);
eva1 = X(1);
figure(1);
fplot(X2,[-0.002 0.002]);
hold on;
fplot(X,[-0.002 0.002]);
hold off;
xlabel('Rabi frequency [THz]','FontSize',11);
ylabel('dressed states','FontSize',11);
grid on;
box on;
I'm expecting the plot for matrix A to just be similar to the plot of matrix B, but somehow it doesn't work properly. I'd appreciate some tips and tricks how to fix this.
The second plot looks like that because the eigenvalues of B are imaginary. When using plot(), it plots the real part of complex numbers by default, but apparently fplot() doesn't. You can do fplot(real(X), [-0.002 0.002]) instead to plot just the real part of the eigenvalues (assuming that's what you want).
I have an upper triangular adjacent matrix which represents a set of nodes which are connected. Every node is defined by three geographical coordinates: x y z.
My goal is to plot the network in order to see what it looks like by taking into account also the direction of the edges.
If I do not take into account the z coordinate, I am able to display the result easily:
The lines of code to get this result are:
A = [0 1 1 0 0 0 0;
0 0 1 1 0 0 0;
0 0 0 1 1 1 0;
0 0 0 0 1 1 0;
0 0 0 0 0 0 1;
0 0 0 0 0 0 1;
0 0 0 0 0 0 0];
xyz = [ 0 0 0;
-15 20 5;
17 24 -3;
-5 36 7;
-14 50 -8;
16 56 3;
3 70 -1];
F = digraph(A);
figure
p = plot(F,'XData',xyz(:,2),'YData',xyz(:,1)); axis equal;
highlight(p,1,'NodeColor','g'); highlight(p,size(A,1),'NodeColor','r');
view([0 90])
How should I modify my code in order to assign to the graph also the z coordinates so I can have a 3d graph? (remember I want to display the edge direction too!!).
What I tried to do is this:
p = plot3(F,'XData',xyz(:,2),'YData',xyz(:,1),'ZData',xyz(:,3));
but I had no success.
Cool problem. I had some spare time and produced this:
close all
clear all
A = [0 1 1 0 0 0 0;
0 0 1 1 0 0 0;
0 0 0 1 1 1 0;
0 0 0 0 1 1 0;
0 0 0 0 0 0 1;
0 0 0 0 0 0 1;
0 0 0 0 0 0 0];
xyz = [ 0 0 0;
-15 20 5;
17 24 -3;
-5 36 7;
-14 50 -8;
16 56 3;
3 70 -1];
figure; hold on
for jj=1:size(A,1) %cycle on nodes
conn=find(A(jj,:)); %find connections for each node
if numel(conn>0) %if there are non null connections
for kk=1:numel(conn) %plot them
a=conn(kk);
lh=quiver3(xyz(jj,1),xyz(jj,2),xyz(jj,3),...
xyz(a,1)-xyz(jj,1),xyz(a,2)-xyz(jj,2),xyz(a,3)-xyz(jj,3),0,'maxheadsize',0.5);
set(lh,'linewidth',4);
set(lh,'color',[1,0,0]);
end
end
end
scatter3(xyz(:,1),xyz(:,2),xyz(:,3),800,'b','.') %plot nodes
%number the nodes
for ii=1:size(xyz,1)
text(xyz(ii,1),xyz(ii,2),xyz(ii,3),num2str(ii),'Color','k','FontWeight','bold',...
'FontSize',14, 'HorizontalAlignment','right', 'VerticalAlignment','bottom')
end
xlabel('x')
ylabel('y')
zlabel('z')
view(-15,18)
grid on
That I think looks pretty much like what you want:
EDIT
If you want the arrow head to be at the midpoint of the two nodes you can use:
line([xyz(a,1) xyz(jj,1)],[xyz(a,2) xyz(jj,2)],[xyz(a,3) xyz(jj,3)],'color',[1 0 0],'linewidth',3)
lh=quiver3(xyz(jj,1),xyz(jj,2),xyz(jj,3),...
xyz(a,1)-xyz(jj,1),xyz(a,2)-xyz(jj,2),xyz(a,3)-xyz(jj,3),.5,'maxheadsize',0.5);
This draws a line first and the superimpose an arrow that reaches the midpoint (note the 0.5scaling factor in the quivercommand):
In the newest MATLAB Release (R2016b), it's now possible to plot a node-link graph in 3d by choosing a different layout method, or specifying x, y and z coordinates directly. In your example, replace the plotting line by
p = plot(F,'XData',xyz(:,2),'YData',xyz(:,1),'ZData',xyz(:,3));
Resulting plot:
I'm writing a MATLAB code, I encountered a problem: I have a (2N+1)*(2N+1) matrix for example 7*7. I want to assign coordinate system to it such that the matrix center is the origin of coordinate system. I mean I want to assign (0,0) to row 4 and column 4 of matrix, (1,0) to row 4 and column 5 of matrix and so on. please help me
Thank you in advance
I want to generate a line of ones in all possible directions in a square matrix like this:
0 0 0 0 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 1 0
0 0 0 1 0 0 0
0 1 0 0 0 0 0
1 0 0 0 0 0 0
0 0 0 0 0 0 0
center of matrix is the origin. this line has 30 degree from horizontal axis.
What you want is a simple mapping from the original matrix counting system to a customized one. Here I have built two cell matrices, representing the coordinates of the elements in the matrix.
Here I have done a simple mapping as follows:
for ii = 1:7
for jj=1:7
D{ii,jj} = C{ii,jj} - [4,4];
end
end
Generally, for matrix of size 2*N+1, you will do the following:
for ii = 1:2*N+1
for jj = 1:2*N+1
D{ii,jj} = C{ii,jj} - [N+1,N+1];
end
end
where C is the original matrix and D is the mapped matrix. After you well-understood what I have done here, you can then replace the for-loops with more efficient functions such as bsxfun.
Good day,
In Matlab I have got a matrix which is very sparse. Now I would like to plot the 'density' of the matrix. Let's say I have a matrix A:
A = [3 0 0
0 2 0
0 0 1];
Now the plot should look something like:
x
x
x
So there should be a dot (or something else) at each location (row, column) in which matrix A has got a nonzero value.
Any ideas?
spy is what you need:
% taken from MatLab documentation
B = bucky;
spy(B)
Consider something like this:
subs = zeros(0,2);
for ind = [find(A)']
[r,c] = ind2sub(size(A), ind);
subs = [subs; [r,c]];
end
scatter(subs(:,2), subs(:,1));
set(gca,'YDir','Reverse')
xlim([1 size(A,2)])
ylim([1 size(A,1)])
Which, for the matrix A:
0 1 0 1 1
0 0 0 0 0
0 1 0 0 0
0 1 0 1 1
0 0 1 1 0
Gives you the following scatter plot:
What about this :
A=[3 0 0; 0 2 0; 0 0 1];
imagesc(A)
I have the following 5x5 Matrix A:
1 0 0 0 0
1 1 1 0 0
1 0 1 0 1
0 0 1 1 1
0 0 0 0 1
I am trying to find the centroid in MATLAB so I can find the scatter matrix with:
Scatter = A*Centroid*A'
If you by centroid mean the "center of mass" for the matrix, you need to account for the placement each '1' has in your matrix. I have done this below by using the meshgrid function:
M =[ 1 0 0 0 0;
1 1 1 0 0;
1 0 1 0 1;
0 0 1 1 1;
0 0 0 0 1];
[rows cols] = size(M);
y = 1:rows;
x = 1:cols;
[X Y] = meshgrid(x,y);
cY = mean(Y(M==1))
cX = mean(X(M==1))
Produces cX=3 and cY=3;
For
M = [1 0 0;
0 0 0;
0 0 1];
the result is cX=2;cY=2, as expected.
The centroid is simply the mean average computed separately for each dimension.
To find the centroid of each of the rows of your matrix A, you can call the mean function:
centroid = mean(A);
The above call to mean operates on rows by default. If you want to get the centroid of the columns of A, then you need to call mean as follows:
centroid = mean(A, 2);