How to format data for use in nntool (MATLAB)? - matlab

In nntool, the sample data is formatted as: [0 1 -1; 2 3 1]
I have ~8000 data points in a text file.
How do I format those points for use here? What does the semicolon signify?

From this example, that would mean each column of the input data would be separated by a ;. The Target data would be a vector like [1 2 3 4] corresponding to each row of the input data.
E.g. if you want to learn the XOR truth table:
X Y XOR
0 0 0
0 1 1
1 0 1
1 1 0
Then the Input matrix is
X Y
0 0
0 1
1 0
1 1
And the Target is
XOR
0
1
1
0
And therefore, your data should be formatted as [0 0 1 1;0 1 0 1] for the input (each column is separated by a ;) and the target data would be [0 1 1 0].
As far as your 8000 point data file is concerned, you can load it into a variable in your workspace and let nntool read the input matrix from your workspace or a .mat file (after you've saved the variable into it).

Related

Up-/Downsampling of a logical vector (not with zeros)

I hope you can help with a little problem I am having.
I want to upsample and downsample a vector with zeros and ones. We have the functions upsample and downsample for that, however, the upsample function in Matlab only adds zeros to the vector. I would like to repeat the value, instead of just putting in zeros.
Unfortunately the upsample function does not do that. Thus, I tried to use repmat (in the third dimension) and then reshape to get back to the old format. I know it must be possible with these functions, but if I simply use them, the vector just gets duplicated and added to the end.
An example:
The input vector is: [1 0 0 1 0 1 0 1 1 1 0 0] (these should be random).
Now I want to upsample (say) by a factor of 2. Then I want to get:
[1 1 0 0 0 0 1 1 0 0 1 1 0 0 1 1 1 1 1 1 0 0 0 0].
Thanks in advance for any help!
You can use repelem:
>> repelem([1 0 1],2)
ans =
1 1 0 0 1 1
Or using repmat and reshape when input is a column vector:
>> input = [1 0 1];
>> reshape(repmat(input, 2, 1), 1, [])
ans =
1 1 0 0 1 1

Counting islands in a matrix (preferably in IDL)

How can I count the number of values that are contiguous in an matrix? For example, if
A= [ 1 0 1 0 0 0 0 \
1 1 1 0 0 0 1\
1 1 0 1 1 1 1]
is a 7 by 3 matrix then the result should indicate that there are 12 contiguous values that are "1" (highlighted in bold), that there are 8 contiguous 0 values (highlighted in italics) and one lone 0 value. Code in IDL is preferred but MATLAB would also be helpful.
This is what you can do in MATLAB using the bwconncomp function. This is a function in the Image Processing Toolbox. I don't know a whole lot about IDL, it might have a similar function.
bwconncomp returns a struct with some information, one of the fields is PixelIdxList, which is a cell array with one element per connected component. Each of these elements is a vector with the indices to one of the array elements in the connected component. For the case of the 1 elements in your example, this cell array will have one vector with 12 values. For the case of the 0 elements, it will have two vectors, one with 1 value and one with 8:
>> A = [ 1 0 1 0 0 0 0 ; 1 1 1 0 0 0 1 ; 1 1 0 1 1 1 1 ];
>> CC = bwconncomp(A==1, 8);
>> cellfun(#numel, CC.PixelIdxList)
ans =
12
>> CC = bwconncomp(A==0, 8);
>> cellfun(#numel, CC.PixelIdxList)
ans =
1 8
The bwconncomp takes 4 or 8 as the second argument. This specifies what are considered connected elements (contiguous values, neighbors). 4 means only the 4 elements N,S,E,W are connected; 8 means also diagonal connections exist (8 neighbors).

gaussian elimination of permutation matrix

I have a binary sparse matrix H of size 600*1200 constructed by concatenating square permutation matrices of size 200, thus sparse matrix have 3 ones in each column and 6 ones in each row. Now am trying to transform the matrix into reduced echelon form.
This is my code:
[m,n]=size(H);
for i=1:m
ind=find(H(:,i),1,'last');
if ind<=i
continue;
end
if ind~=i
temp=H(ind,:);
H(ind,:)=H(i,:);
H(i,:)=temp;
end
I=find(H(:,i));
% Guassian elimination
for j=1:length(I)
if I(j)~=i
H(I(j),:)=mod(H(I(j),:)+H(i,:),2);
end
end
end
But whichever H matrix generated, I can't get rid of other entries at 400th column,
how can I fix this, help
Since Gaussian elimination does not involve rearrangement of columns, the first 600 columns of the resulting matrix will only form the identity matrix if the first 600 columns of the original matrix were linearly independent. Otherwise, you are going to have "short" columns with other entries in them, as the Wikipedia article shows.
The way your matrix is structured, the first 400 columns are guaranteed to be linearly dependent. Indeed, the sum of the first 200 columns is the all-1 vector, and so is the sum of the columns 201-400. This is why you are seeing these entries in the 400th column.
To create identity matrix on the left you need to rearrange columns. One way, which looks a bit redundant but is very easy to code, is to
run your algorithm
rearrange columns (it's easy to identify the desired columns after rref)
do rref again.
Here is the code that does steps 2-3.
for i = 1:m
j = find(H(i,:),1,'first');
[H(:,i), H(:,j)] = deal(H(:,j), H(:,i));
end
H = rref(H)
Sample input into rref:
1 0 1 0 1
0 1 0 0 1
1 0 1 1 0
0 0 0 0 1
Output of your code:
1 0 1 1 0
0 1 0 0 1
0 0 0 1 1
0 0 0 0 1
After the column swap and second rref:
1 0 0 0 1
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0

Creating a perceptron network when input array is large

I need to create a perceptron which has two target values(0,1) and 21 input vectors. Each vector is 110592 in size. How do I call the "newp" function for this?
p=[I1,I2,I3,P1,P2,P3,Q1,Q2,Q3,R1,R2,R3,Z1,Z2,Z3,A1,A2,A3,B1,B2,B3];
t=[0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
each vector I1,I2. etc are of size 1*110592.
if the perceptron has only two inputs, i can call the function as below.
newp([0 1;0 2],1); // which says 1st input is of range 0 to 1 and 2nd input is of range 0 2
SO the problem is how do i format my first argument when i have such a large vector input?

Working on Binary Data in Matlab

I have loaded a text file test file in matlab. The load function has now created a variable test with values
1 2 3 4 5
2 3 NaN NaN NaN
Now I have a initialized variable X = [0 0 0 0 0 0 0 0 0 0 0] and Y = [0 0 0 0 0 0 0 0 0 0 0].
I want X to read the first row of test variable and corresponding to the number it reads change its value of the element from 0 to 1. Suppose it reads 1, it should change its element X(1) from 0 to 1. Similarly Y should do the same wrt second row of test.
Any idea how should I proceed?
You can index directly into a vector like so:
x=zeros(1,10);%# your zero vector
a=[1,3,7,8]; %# the index of sports you like
x(a)=1 %# change from 0 to 1
x =
1 0 1 0 0 0 1 1 0 0
Going by your comment, I think this is what you want.