I have been trying to label the edges of an undirected graph, also, i am using this matgraph tool! I succeeded in making a graph, i just want to assign weights to it... Please help!!
Here is what i tried,
clear all;
close all;
clc;
g=graph;
for k=1:6
add(g,k,k+1)
add(g,1,4)
add(g,5,7)
end
ndraw(g);
x=rand(1,1);
y=rand(1,1)
A =[0 x 0 x 0 0 0;
x 0 x 0 0 0 0;
0 x 0 x 0 0 0;
x 0 x 0 x 0 0;
0 0 0 y 0 x x;
0 0 0 0 x 0 x;
0 0 0 0 x x 0]
If I understood you correctly you can add this code after the code you wrote:
% get line info from the figure
lineH = findobj(gca, 'type', 'line');
xData = cell2mat(get(lineH, 'xdata')); % get x-data
yData = cell2mat(get(lineH, 'ydata')); % get y-data
% if an edge is between (x1,y1)<->(x2,y2), place a label at
% the center of the line, i.e. (x1+x2)/2 (y1+y2)/2 etc
labelposx=mean(xData');
labelposy=mean(yData');
% generate some random weights vector
weights=randi(21,length(labelposx),1);
% plot the weights on top of the figure
text(labelposx,labelposy,mat2cell(weights), 'HorizontalAlignment','center',...
'BackgroundColor',[.7 .9 .7]);
Related
I want to make the grid of the smaller mesh, some idea of how it is done?
That is to say something like that the path in the x-axis and y are 0.25 in 0.25,
x = (0: 0.25: 7);
y = (0: 0.25: 7);
I've the following matrix. This matrix modeling the dynamic of a population (projection matrix)
L2=[0 0 0 0 127 4 80;
0.6747 0.7370 0 0 0 0 0;
0 0.0486 0.6610 0 0 0 0;
0 0 0.0147 0.6907 0 0 0;
0 0 0 0.0518 0 0 0;
0 0 0 0 0.8091 0 0;
0 0 0 0 0 0.8091 0.8089];
With this code, I find the eingenvectors rigth and left of L2. Likewise I find the sensivity and elasticity matrix.
A=L2;
[W,lambdas]=eig(A);
V=conj(inv(W));
lambdas=diag(lambdas);
[lambdas,I]=sort(lambdas);
lambdas=flipud(lambdas);
lambda1=lambdas(1);
I=flipud(I);
W=W(:,I);
V=V(I,:);
w=W(:,1);
w=w/sum(w);
v=real(V(1,:))';
v=v/v(1);
% matrix of sensitivity
senmat=v*w';
% matrix of elasticity
emat=senmat.*A/max(eig(A));
Then, I make a surface of sensivity matrix.
surf(senmat)
This is the result:
I need to make the squares (grid) of the surface smaller.
any ideas?
best regards!
YOu can use interp2 if you have (x,y) which are defined for senmat. Read about interp2. If you want only senmat to refine use imresize.
A = imresize(senmat,[100,100]) ;
surf(A)
As the title says, I want to covert a line(or any equation) to 2D matrix.
For example: If I have a line equation y = x, then I want it to be like:
0 0 0 0 0 1
0 0 0 0 1 0
0 0 0 1 0 0
0 0 1 0 0 0
0 1 0 0 0 0
1 0 0 0 0 0
and the length of rows and columns can be variable.
Is there a function or method to implement that?
use meshgrid to get x-y grids:
% set resolution parameters
xmin = 1;
xmax = 100;
dx = 0.1;
ymin = 1;
ymax = 100;
dy = 0.1;
% set x-y grid
[xg, yg] = meshgrid(xmin:dx:xmax, ymin:dy:ymax);
% define your function
f = #(x) (x - 30).^2;
% apply it on the x grid and get close y values
D = abs(f(xg) - yg);
bw = D <= 1;
figure;
imshow(bw);
% flip y axis
set(gca,'YDir','normal')
you get:
and then you can further dilate/erode/skeletonize the output
Given x and y coordinates, how to fill in a matrix with those coordinates? As it is said, just do it in a for loop, or use the "sub2ind" function.
% x,y coordinates
x=0:.01:30;
y=10*sin(2*pi*.1*x);
% add offset so that (x,y)-coordinates are always positive
x=x+abs(min(x))+1;
y=y+abs(min(y))+1;
figure,plot(x,y,'.');axis tight
x=ceil(x); y=ceil(y);
im=zeros(max(y),max(x));
ind=sub2ind(size(im),y,x);
im(ind)=1;
figure,imagesc(im),axis image, axis xy;colormap gray;axis tight
xlabel('x'); ylabel('y')
Why not doing it on your own? You loop through all x-coordinates of the matrix that you (probably scaled) use as the x for your function and get an y out, that you (probably scaled) can round and then use as a y coordinate for your matrix to set the 1.
What is the simplest way to create the following plot in Matlab from the data of the matrix "positions" containing the value 1 for black marks and - 1 for white marks?
positions=[0 1 0 0 0 0; 0 -1 0 0 0 0; 0 1 0 0 0 0; 1 -1 1 1 -1 0]
There you go:
% //Data
positions = [0 1 0 0 0 0; 0 -1 0 0 0 0; 0 1 0 0 0 0; 1 -1 1 1 -1 0];
S = 50; %// circle size. Adjust manually
%// Preparation
[M N] = size(positions);
hold on
%// Plot filled circles
[ip jp] = find(flipud(positions)>0);
plot(jp-.5,ip-.5,'ko','markersize',S,'markerfacecolor','k')
%// Plot empty circles
[in jn] = find(flipud(positions)<0);
plot(jn-.5,in-.5,'ko','markersize',S)
%// Plot grid lines
plot([0 N],(1:M).'*[1 1],'k');
plot((1:N).'*[1 1],[0 M],'k');
%// Set tick labels
set(gca,'xtick',.5:N,'ytick',.5:M)
set(gca,'xticklabel',char((1:N).'+'A'-1),'yticklabel',char((1:M).'+'0'))
%// Set axis size and aspect
axis equal
axis([0 N 0 M])
set(gca,'ticklength',[0 0]) %// no visible ticks
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 want to assign weights to the edges such that the Sum of (the weights coming to a node) and its own weight add to one.
here is what I tried:
clear all;
close all;
clc;
%% building the graph
g=graph;
for k=1:6
add(g,k,k+1)
add(g,1,4)
add(g,5,7)
end
%%assigining the statuses 0 and 1
%label(g,1,'0');
%label(g,2,'1');
%label(g,3,'1');
%label(g,4,'1');
%label(g,5,'1');
%label(g,6,'0');
%label(g,7,'0');
figure,ldraw(g);
%x=rand(1,1);
%y=rand(1,1)
%% get line info from the figure
lineH = findobj(gca, 'type', 'line');
xData = cell2mat(get(lineH, 'xdata')); % get x-data
yData = cell2mat(get(lineH, 'ydata')); % get y-data
%% if an edge is between (x1,y1)<->(x2,y2), place a label at
%%the center of the line, i.e. (x1+x2)/2 (y1+y2)/2 etc
labelposx=mean(xData');
labelposy=mean(yData');
%% generate some random weights vectori.e. the probability matrix
weights=rand(1,1,length(labelposx))
% plot the weights on top of the figure
text(labelposx,labelposy,mat2cell(weights), 'HorizontalAlignment','center',...
'BackgroundColor',[.7 .9 .7]);
%%Transition matrix or markov matrix
% Transition=[0 (1,2) 0 (1,4) 0 0 0;
% (2,1) 0 (2,3) 0 0 0 0;
% 0 (3,2) 0 (3,4) 0 0 0;
% 0 0 (4,3) 0 (4,5) 0 0;
% 0 0 0 (5,4) 0 (5,6) (5,7);
% 0 0 0 0 (6,5) 0 (6,7);
% 0 0 0 0 (7,5) (7,6) 0];
Transition= [0 weights(:,:,8) 0 weights(:,:,6) 0 0 0;
weights(:,:,8) 0 weights(:,:,7) 0 0 0 0;
0 weights(:,:,7) 0 weights(:,:,5) 0 0 0;
weights(:,:,6) 0 weights(:,:,5) 0 weights(:,:,4) 0 0;
0 0 0 weights(:,:,4) 0 weights(:,:,3) weights(:,:,2);
0 0 0 0 weights(:,:,3) 0 weights(:,:,1);
0 0 0 0 weights(:,:,2) weights(:,:,1) 0]
%set_matrix
%%dij-- Probability matrix
sparse(Transition);
d=[weights(:,:,8);weights(:,:,7);weights(:,:,5);weights(:,:,4);
weights(:,:,3);weights(:,:,1);weights(:,:,1)]
%%Si[k]-- matrix of the statuses(labels)
%S=[0 1 1 1 1 0 0]
For eg: The addition of weights coming to node four, plus its own weight should be equal to 1
Check out this nice random vectors generator with fixed sum FEX file. I think this will answer your question.
see also more on this SO link Non biased return a list of n random positive numbers (>=0) so that their sum == total_sum