Draw network or graph from matrix in matlab - matlab

How do I draw a sequence of frames of a network with the help of a transition matrix?
I have a matrix that denotes a graph. The matrix changes with iterations. Can anyone give me an insight of what functions I can use to create the series of the network?
original=[0.06 0.57 0.37 0 0;
0.57 0.06 0.37 0 0;
0.37 0.57 0.03 0.03 0;
0 0 0.03 0.13 0.84;
0 0 0 0.84 0.16];
Suppose the, above is the matrix in question. Then the graph should be

This question is related to this earlier query and this one. But here's an answer specific to your situation.
Given a weighted adjacency matrix:
original = [0.06 0.57 0.37 0 0;
0.57 0.06 0.37 0 0;
0.37 0.57 0.03 0.03 0;
0 0 0.03 0.13 0.84;
0 0 0 0.84 0.16];
you can first define the number of nodes in the network:
N = size(original,1);
and then a corresponding set of coordinates on the perimeter of a circle:
coords = [cos(2*pi*(1:N)/N); sin(2*pi*(1:N)/N)]';
Then you can plot the graph using gplot:
gplot(original, coords)
and mark the vertices using text:
text(coords(:,1) - 0.1, coords(:,2) + 0.1, num2str((1:N)'), 'FontSize', 14)
Note that the gplot function does not weight the lines by connection strength; the matrix element (i,j) is treated as binary, indicating absence or presence of a link between nodes i and j.

Related

How to interpolate matrix to get specific values

I have this matrix in MATLAB:
x = [NaN -2 -1 0 1 2;
1 0.21 0.15 0.34 0.11 0.32;
2 0.14 0.10 0.16 0.31 0.11];
The first row represents the location of the values following X coordinates.
I shift the first row by -0.63, so x becomes:
New_x = [NaN -2.63 -1.63 -0.63 0.37 1.37;
1 0.21 0.15 0.34 0.11 0.32;
2 0.14 0.10 0.16 0.31 0.11];
How can I use interpolation to get the values at specific coordinates of the New_x matrix that we have in the x matrix? ([-2 -1 0 1 2] points)
New_xInterp = [NaN -2.63 .. -2 .. -1.63 .. -1 .. -0.63 .. 0 .. 0.37 .. 1 .. 1.37 .. 2;
1 0.21 .. ? .. 0.15 .. ? .. 0.34 .. ? .. 0.11 .. ? .. 0.32 .. ?;
2 0.14 .. ? .. 0.10 .. ? .. 0.16 .. ? .. 0.31 .. ? .. 0.11 .. ?];
I want to get the '?' values. I tried to use interp2 function but I don't know which step or 2^k-1 interpolated points between coordinates values I have to have in order to get the points like -2, -1, 0, 1, 2.
Thanks !
Since you do not have 2D data, you are only interpolating on one dimension, you only need the function interp1.
This function can work on vector or matrices if necessary, but it require a slight reorganisation of your data.
%% Input
M = [NaN -2 -1 0 1 2;
1 0.21 0.15 0.34 0.11 0.32;
2 0.14 0.10 0.16 0.31 0.11];
%% Demultiplex inputs
x = M(1,2:end).' ; % extract X values, reorder in column
y = M(2:end,2:end).' ; % extract Y values, reorder in columns
%% Interpolate
xn = sort( [x-0.63 ; x] ) ; % Generate the new_x target values
yn = interp1( x-0.63 , y , xn ,'linear','extrap') ; % Interpolate the full matrix in one go
At this point you have your new xn and yn values in columns:
xn= yn=
-2.63 0.21 0.14
-2 0.1722 0.1148
-1.63 0.15 0.1
-1 0.2697 0.1378
-0.63 0.34 0.16
0 0.1951 0.2545
0.37 0.11 0.31
1 0.2423 0.184
1.37 0.32 0.11
2 0.4523 -0.016
I would keep them like that if you have more operations to do on them later on. However, if you want it back into the format you had at the beginning, we can simply rebuild the new full matrix:
%% Rebuild global matrix
Mout = [ M(:,1) , [xn.' ; yn.'] ]
Mout =
NaN -2.63 -2 -1.63 -1 -0.63 0 0.37 1 1.37 2
1 0.21 0.1722 0.15 0.2697 0.34 0.1951 0.11 0.2423 0.32 0.4523
2 0.14 0.1148 0.1 0.1378 0.16 0.2545 0.31 0.184 0.11 -0.016
Maybe you can try interp1 + arrayfun like below
r = sort([x(1,2:end),New_x(1,2:end)]);
New_xInterp = [New_x(:,1),cell2mat(arrayfun(#(k) interp1(New_x(1,2:end),New_x(k,2:end),r),1:size(New_x,1),'UniformOutput',false).')];
which gives
New_xInterp =
NaN -2.63000 -2.00000 -1.63000 -1.00000 -0.63000 0.00000 0.37000 1.00000 1.37000 NA
1.00000 0.21000 0.17220 0.15000 0.26970 0.34000 0.19510 0.11000 0.24230 0.32000 NA
2.00000 0.14000 0.11480 0.10000 0.13780 0.16000 0.25450 0.31000 0.18400 0.11000 NA
The code above used linear interpolation. If you want other options, you can type help interp1 to see more.

Octave - why is surf not working but trisurf does?

I am able to plot a trisurf chart, but surf does not work.
What am I doing wrong?
pkg load statistics;
figure (1,'name','Matrix Map');
colormap('hot');
t = dlmread('C:\Map3D.csv');
tx =t(:,1);ty=t(:,2);tz=t(:,3);
tri = delaunay(tx,ty);
handle = surf(tx,ty,tz); #This does NOT work
#handle = trisurf(tri,tx,ty,tz); #This does work
`error: surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length
(X)
My data is in a CSV (commas not shown here)
1 2 -0.32
2 2 0.33
3 2 0.39
4 2 0.09
5 2 0.14
1 2.5 -0.19
2 2.5 0.13
3 2.5 0.15
4 2.5 0.24
5 2.5 0.33
1 3 0.06
2 3 0.44
3 3 0.36
4 3 0.45
5 3 0.51
1 3.5 0.72
2 3.5 0.79
3 3.5 0.98
4 3.5 0.47
5 3.5 0.55
1 4 0.61
2 4 0.13
3 4 0.44
4 4 0.47
5 4 0.58
1 4.5 0.85
surf error message is different in Matlab or in Octave.
Error message from Matlab:
Z must be a matrix, not a scalar or vector.
The problem is pretty clear here since you specified Z (for you tz) as a vector.
Error message from Octave:
surface: rows (Z) must be the same as length (Y) and columns (Z) must be the same as length (X)
You are wrong here since on your example, columns (Z) = 1, but length (X) = 26, so here is the mistake.
One of the consequences of that is that with surf you cannot have "holes" or undefined points on your grid. On your case you have a X-grid from 1 to 5 and a Y-grid from 2 to 4.5 but point of coordinate (2, 4.5) is not defined.
#Luis Mendo, Matlab and Octave do allow the prototype surf(matrix_x, matrix_y, matrix_z) but the third argument matrix_z still have to be a matrix (not a scalar or vector). Apparently, a matrix of only one line or column is not considered as a matrix.
To solve the issue, I suggest something like:
tx = 1:5; % tx is a vector of length 5
ty = 2:0.5:4.5; % ty is a vector of length 6
tz = [-0.32 0.33 0.39 0.09 0.14;
-0.19 0.13 0.15 0.24 0.33;
0.06 0.44 0.36 0.45 0.51;
0.72 0.79 0.98 0.47 0.55;
0.61 0.13 0.44 0.47 0.58;
0.85 0. 0. 0. 0.]; % tz is a matrix of size 6*5
surf(tx,ty,tz);
Note that I had to invent some values at the points where your grid was not defined, I put 0. but you can change it with the value you prefer.

How can I find the average of largest set of non-zero values in an array

How can I find the the starting point of A array and calculate average starting from starting points to 1 second
A=[0 0 0 0 0 -0.01 -0.2 0.3 0.4 0.5 0 0 0 0 0 0 0.01 0.02 0.03 0.04 0.1 0.2 0.3 0.4 0.7 0.8 1 1.2 1.3 1.4 1.5]
Time=[0 0.1 .2 .3 .4 .5 .6 .7 .8 .9 1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.8 3 3.1]
By removing the noise the starting point should be A(17) which is equal to 0.01
Then calculate average of A starting from starting point after 1 seconds
Code is self explanatory
A=[0 0 0 0 0 -0.01 -0.2 0.3 0.4 0.5 0 0 0 0 0 0 0.01 0.02 0.03 0.04 0.1 0.2 0.3 0.4 0.7 0.8 1 1.2 1.3 1.4 1.5] ;
Time=[0 0.1 .2 .3 .4 .5 .6 .7 .8 .9 1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.8 3 3.1];
%make negative values zero
A(A<0)=0;
%get non negative values position and add padding
mask=[0,A>0,0];
%get starting points
startingPoints =strfind(mask,[0 1]);
%get length of continuous values from starting points
temp =diff(find(~mask))-1;
length = temp(temp>0);
%get the index of largest length
[~,index]=max(length);
%get starting point
dataStartingIndex = startingPoints(index)
%starting point value
A(dataStartingIndex)
%get ending point after 1 seconds
dataEndingIndex=find((Time(dataStartingIndex)+1)==Time);
%find average
avg=mean(A(dataStartingIndex:dataEndingIndex))
This really depends on your data. It is a bit unclear but in your example it seems that noise can exceed your 'information value'. So you can't detect it just with a threshold.
Maybe get the position where A is always superior to something like 0.01 :
startpos= (A>0).argmax()
truedata=A[startpos:]
time=T[startpos:]
you can calculate average with the method .mean()

2D interpolation with interp2

I want to describe surface flatness of a plane with interp2 function.
Spatial sampling points are as below.
width=[0 500];
length=[0 100 200 300 400 500 600 700 800 900 1000];
and flatness are as below, at width 0 and 500, respectively.
a = [1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1]; % flatness at width 0
b = [-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1]; % flatness at width 500
With these values, the surface shape will be like following figure.
I wanted to change this figure into following figure with interp2 function.
Below is my code.
widthq=[0 100 200 300 400 500];
flatness=[a' b'];
flatnessq=interp2(width,length,flatness,widthq,length);
But, not working with one error, 'The input data has inconsistent size.'
Can anyone explain this error and give a way how to interpolate my data with interp2 function?
The first three inputs need all to be of the same size
[W, L] = meshgrid(width, length);
The last two arguments need also to be of same size
[Wq, Lq] = meshgrid(widthq, length);
Then it should work
flatnessq=interp2(W,L,flatness,Wq,Lq);

understanding loops in matlab

The following code subtracts tolerance after it chooses a random number from each row. I have made a slight mistake here. I don't want to subtract tolerance of the diagonal elements from the transition. how do i fix that?
any help is appreciated.
clear all;
close all;
clc;
tolerance= 0.01;
Transition = [0.06 0.47 0 0.47 0 0 0;
0.47 0.06 0.47 0 0 0 0;
0 0.47 0.06 0.47 0 0 0;
0.47 0 0.47 0.037 0.023 0 0;
0 0 0 0.023 0.037 0.47 0.47;
0 0 0 0 0.47 0.06 0.47;
0 0 0 0 0.47 0.47 0.06];
len=length(Transition);
Dij=Transition;
% Assigned status of all the sites at given time k
S_k= [0 1 1 1 1 0 0];
for i=1:7
while(1)
sel=randi(7);
if(Dij(i,sel)~=0)
show(i)=sel;
break;
end
end
Dij(i,sel)=Dij(i,sel)-tolerance;
end
I want to carry this loop till one of the non diagonal elements is zero.
If I have understood correctly then you only need to change this line
if(Dij(i,sel)~=0)
To include the condition that it is also not on the diagonal
if((Dij(i,sel) ~= 0) && (i ~= sel))
unless you still want to call show() on the diagonals and just not do the subtration in which case just move that logic lower down i.e.
Dij(i,sel)=Dij(i,sel)-tolerance*(i ~= sel);
but also just one more comment. while(1) break; isn't a great construct. You could just have while(Dij(i, sel)~= 0) and Dij(i, sel) = 0 just above and avoid the need to call break at all.
Lastly my understanding is you have opened yourself up to an infinite loop if all columns are 0. Maybe this can never happen but just something to think about.