PyTables table.where equivalent in matlab - matlab

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),:)

Related

Error when running G= graph(s,t) in matlab

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.

What is the easiest way on a binary image to check, if two pixels are connected? (In Matlab)

Consider this binary image:
0 1 0 0 0
0 1 0 0 0
0 1 1 0 0
0 0 0 0 0
0 0 0 1 0
I am looking for a function with two coordinates as parameters, and a boolean return value, which states, if the two pixels are connected (by 4- or 8-connectivity), like this:
f([1,2],[3,3]) -> true;
f([1,2],[5,4]) -> false;
I know, that there must be an easy algorithm, and there are some functions in Matlab, which do much more (bwdist, bwconncomp), but I'm looking for a simpler way.
Thanks for the help!
Your alternatives are to flood fill from one pixel, then check the other, to label all connected components and check the label, or to do A* pathfinding. A* will probably produce the fastest results if most of the pairs are close together but in large shapes, it's also the most complicated of the three methods.
Matlab has labelconnected components built in. It's not a particularly complicated algorithm. If you check my binary image processing library you can find implementations in C of all three methods.
https://github.com/MalcolmMcLean/binaryimagelibrary

Choosing which variables to normalize while applying logistic regression

Suppose a dataset comprises independent variables that are continuous and binary variables. Usually the label/outcome column is converted to a one hot vector, whereas continuous variables can be normalized. But what needs to be applied for binary variables.
AGE RACE GENDER NEURO EMOT
15.95346 0 0 3 1
14.57084 1 1 0 0
15.8193 1 0 0 0
15.59754 0 1 0 0
How does this apply for logistic regression and neural networks?
If the range of continuous value is small, encode it into a binary form and use each bit of that binary form as a predictor.
For example, number 2 = 10 in binary.
Therefore
predictor_bit_0 = 0
predictor_bit_1 = 1
Try and see if it works. Just to warn you, this method is very subjective and may or may not yield good results for your data. I'll keep you posted if I find a better solution

read arrays in Simulink

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.

comparing images matlab

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.