I want to calculate L = laplacian(G) from a graph dataset. I imported the dataset which contains two columns: FromNodeId and ToNodeId:
# Nodes: 3997962 Edges: 34681189
# FromNodeId ToNodeId
0 1
0 2
0 31
0 73
0 80
0 113619
0 2468556
0 2823829
0 2823833
0 2846857
0 2947898
0 3011654
0 3701688
0 3849377
0 4036524
0 4036525
0 4036527
0 4036529
0 4036531
0 4036533
0 4036534
0 4036536
0 4036537
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
To do so, I need to find G first so I use G = graph(FromNodeId, FromNodeId). When I did that, I got this error:
>> G = graph(fromNodeId,toNodeId)
Error using matlab.internal.graph.MLGraph
Source must be a dense double array of node indices.
Error in matlab.internal.graph.constructFromEdgeList (line 125)
G = underlyingCtor(double(s), double(t), totalNodes);
Error in graph (line 264)
matlab.internal.graph.constructFromEdgeList(...
I don't know why! Can I get a solution of that? Thank you.
Turns out the problem lies in the fact that no zeros are allowed when using the Graph function in this manner. (See: Target must be a dense double array of node indices. How to Solve?)
I downloaded the dataset and ran it successfully with the following code. Note that this code uses a system command and is not compatible with all operating systems, but it should be simple enough to rewrite to whatever operating system you use. It also assumes the .txt file to be in the working directory.
% Removes first lines with comments in them; this system command was tested on Linux Ubuntu 14.04 and is probably not portable to Windows.
% If this system command doesn't work, manually remove the first four lines from the text file.
system('tail -n +5 com-lj.ungraph.txt > delimitedFile.txt');
% Read the newly created delimited file and add 1 to all nodes.
edges=dlmread('delimitedFile.txt')+1;
% Build the graph
G=graph(edges(:,1),edges(:,2));
Assuming you've build your arrays similarly to how I did it, adding 1 to FromNodeIdFull and ToNodeIdFull should resolve your problem. In other words, the following code snippet should solve your problem; if it doesn't I advise you to rewrite based on the code presented above.
G=graph(FromNodeIdFull+1,ToNodeIdFull+1);
Leaving my old answer here, as deleting it may cause confusion for others reading both this answer and the comments to it. Note that the answer below did NOT resolve the issue.
Just putting the comments by myself and NKN into an answer:
The problem lies in the fact that the arrays are sparse but graph() seems to expect full arrays. The following should work:
FromNodeIdFull=full(double(FromNodeId));
ToNodeIdFull=full(double(ToNodeId));
G=graph(FromNodeIdFull,ToNodeIdFull);
Depending on whether your input arrays are already doubles or not you may be able to remove the double() from the first two lines.
Related
I need some help of solving that issue: I have 5 different voltage values that change every single tick time - that mean every single moment. I need to sort them and after they been sorted I want to go to another matrix(like this one at the bottom) and to pull out(read) specific column from it, for every state pre define(timing that I am designing..) That mechanism change every single states/moment. How can I do this ?
The Matrix look like(and could be greater...):
0 0 0 1 1 1...
0 1 1 0 0 1...
1 0 1 0 1 0...
1 1 0 1 0 0...
.. .. .. .. .. ..
Thanks, Henry
I am not sure I understood it correctly. So I will edit my answer after you make your question a bit more clear.
I see two separate things:
Reading 5 voltage values which change at each step. You want to sort these values. To do this you can use the sort function of matlab. It is really easy to use and you can look at it here.
This is the part I didn't understand well. After sorting the voltage readings what do you want to do with the matrix ? If you want to access just a specific column of the matrix and save it in a variable you can do it in this way. Let's assume you have a matrix A which is N x N, if you want to access the 10th column of the matrix and store it in a variable called column10 you will do something like: column10 = A(:,10)
I hope this will help you but let me know if this is what you wanted and I will edit my answer according to it.
Fab.
I need a fast way in Matlab to do something like this (I am dealing with huge vectors, so a normal loop takes forever!):
from a vector like
[0 0 2 3 0 0 0 5 0 0 7 0]
I need to get this:
[NaN NaN 2 3 3 3 3 5 5 5 7 7]
Basically, each zero value is replaced with the value of the previous non-zero one. The first are NaN because there is no previous non-zero element
in the vector.
Try this, not sure about speed though. Got to run so explanation will have to come later if you need it:
interp1(1:nnz(A), A(A ~= 0), cumsum(A ~= 0), 'NearestNeighbor')
Try this (it uses the cummax function, introduced in R2014b):
i1 = x==0;
i2 = cummax((1:numel(x)).*~i1);
x(i1&i2) = x(i2(i3));
x(~i2) = NaN;
Just for reference, here are some similar/identical functions from exchange central and/or SO columns.
nearestpoint ,
try knnimpute function.
Or best of all, a function designed to do exactly your task:
repnan (obviously, first replace your zero values with NaN)
I had a similar problem once, and decided that the most effective way to deal with it is to write a mex file. The c++ loop is extremely trivial. After you'l figure out how to work with mex interface, it will be very easy.
Ok so let's say i have a binary image containing the pixel representation for 1,2,A,B or whatever. But for now let's just consider 1
0 0 0 0
0 1 1 0
0 1 1 0
0 1 1 0
0 1 1 0
0 0 0 0
and then i have another image containing the standard representation of 1.
Now what i wan't is to compare these two images and decide whether my first image contains pixel values for 1 or not.
What kind of algorithms are available at my disposal ?
Please i do not require the name of the matlab function for image comparison as has been the answer for similar questions. Rather than that i require the name of some algorithms that can be used to solve this problem so that i can implement it on my own in C#
What you need to compute is the distance between your image and the ground truth. This distance can be stated in many different ways. Search google for similarity measures on binary data. See here a review.
Good Morning everybody,
it's sometime I wonder if it is possible to do something close to what I'm gonna describe by means of Matlab:
Using an external tool (i.e. Ansys, Abaqus or other software) I engender a "seed" listed (file extension .inp, .db or others) file which will be used as reference for the next steps;
Starting from this seed listed file, I would like to get, let's say, 200 similar project files, containing some slight variations compared to the seed: I mean, for instance, simulation time or any other characteristic.
I will give a short example: I'm currently working on Bladed, software perfoming aero-elastic simulation for wind energy applications; Blade gives me, for instance, the chance to generate turbulent wind fields. The seed code would be the one shown below:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<BladedProject version="4.2.0.46">
<BladedData dataFormat="project">
<![CDATA[
VERSION 4.2.0.46
MULTIBODY 1
CALCULATION 3
OPTIONS 0
PROJNAME
DATE
ENGINEER
NOTES ""
PASSWORD
MSTART WINDND
SPMODEL 7
NLAT 31
NVER 45
LATDIM 150
VERDIM 220
LONGLS 340.2
LATLS 42.1482
VERTLS 42.1467
XLV 113.4
YLV 66.3117
ZLV 33.1546
XLW 27.72
YLW 25.228
ZLW 50.4542
LAMBDA1 0
CohScale 340.2
COHDEC 12
SCALE 0
GAMMA 0
YDIML 0
N2 0
YDIMS 0
K1MIN 3
LENGTH 1830
STEP .2233905
UBAR 3
SEED 3
OUTFILE l:\02_turb_dev\50-1\loads\50-1_D116_Validation_adapted_to_AV07\wind\DLC1-2_Kaimal\s1\3.wnd
DIAM 0
HUBHT 0
TURBHTTYPE 0
TURBBOTTOM 0
GUSTAVT 0
GUSTSPEED 0
TOLERANCE 0
DLONGMIN 0
DLONGMAX 0
Z0MIN 0
Z0MAX 0
MAXITER 14
MAXSEED 100
NFILES 1
UseWindShear 0
UseShearToGust 0
WVMODEL 0
MATCHFILE ''
SPACING 0
SAMPLEFREQ 0
MEANSPEED 0
ILAT 0
IVERT 0
GUSTMETHOD 0
DLONG 0
ILAT 0
IVERT 0
LONGGUST 0
LATGUST 0
VERTGUST 0
iLONGGUST 0
iLATGUST 0
iVERTGUST 0
PEAKINESS 0
MAXFRAN 0
MEND
0WINDND
]]>
</BladedData>
</BladedProject>
By means of matlab I would like to be able to generate similar project files for different wind speeds and random seeds (UBAR,SEED) and to save these files in a predetermined sub-folder.
Finally, I only would be glad to know if any of you has a clue/advice to give me.
Then, it will be my task to find out a proper architecture for coding.
I thank you all in advance for supporting.
Best regards,
Francesco
So here are some bits and pieces of an answer, read more about the functions used in the Matlab documentation. The code below is only an example, but you shouldn't have any trouble expanding it. This code will write 5 files with UBAR values 1..5. I'll suppose that, at the start of execution, your current working directory is a suitable place for those 5 files to reside, at least temporarily
for ubar = 1:5 %using the name of the variable as the iteration variable here
fname = ['bladefile' num2str(ubar) '.txt'] % filenames will be bladefile1.txt etc
fid = fopen(fname,'w') % open the file for writing, keep the file id for later reference
fprintf(fid, '<?xml version="1.0" encoding="ISO-8859-1" ?>\n<BladedProject version="4.2.0.46">\n <BladedData dataFormat="project">\n <![CDATA[\n')
This fprintf call writes to the output file (using the file id fid); it writes your XML header. Note the embedded \ns which will be turned into new lines in the output file.
fprintf(fid, 'VERSION 4.2.0.46\n') % note, again, the trailing newline so the next line of output starts on a new line
...
fprintf(fid, 'ubar %i\n', ubar)
This call to fprintf writes the string ubar to the file, then the %i indicates that an integer will be written, and the value of that integer will be taken from the variable ubar.
And so on until the end of the loop
fclose(fid)
end % and loop around
This should get you started and, if your aim is only to write a set of output files once, is good enough for throwaway use.
If you expect to re-use the code do as you would in any programming language and wrap it up into a function. For example, you could write a function which takes a value for ubar and a value for seed as inputs and writes the file, something like this;
for ubar = 1:5
for seed = 1:7
write_inp_file(ubar,seed)
end
end
I'm trying to find something similar in MATLAB to PyTables' table.where that selects a subset of a dataset based on criteria (such as col1 = 4). So far, my searching has been completely fruitless. I can't believe such a useful feature wouldn't be supported somehow... can anyone help?
MATLAB ver R2011b.
EDIT: In case it wasn't clear from the question, I'm using an HDF5 file for data storage in MATLAB, hence my desire to find functionality similar to PyTables.
I think what you try to do involves either load-ing the file in memory (or you might give HDF5 Diskmap Class a try if it's to big for memory).
Once you have access to your data in matlab as a matrix, it's easy as:
a=[
0 0 0 0 1;
0 1 0 0 1;
1 0 1 1 1;
0 1 1 1 1;
1 0 1 0 1];
a(find(a(:,1)==1),:)