matlab: zero loop in neural network - matlab

everyone! I wanna have neural network with loop. I create neural network
net = newff(rand(5, 100), rand(1, 100), [3, 2], { 'tansig' 'tansig'}, 'traingdx3', 'learngdm', 'mse');
net.layerConnect(1, 1) = 1;
net.layerWeights{1, 1}.delays = [1];
net.trainParam.epochs = 100;
net = train(net, rand(5, 100), rand(1, 100));
and
net.LW
ans =
[3x3 double] [] []
[2x3 double] [] []
[] [1x2 double] []
but when I view net.LW{1, 1} , I get
net.LW{1, 1}
ans =
0 0 0
0 0 0
0 0 0
why loop weights always zeros?

Related

Refine ANN result precision

I've a neural network in MATLAB that works well, but it should give me a result of 0 or 1, and it gets +/-0.05 error. (0.02, 0.97, 1.03, ...)
What should I change for the result to be more accurate?
p = [ 0 0 1 1; 0 1 0 1];
t = [0 1 1 0];
net = feedforwardnet(2,'trainlm');
net.trainParam.goal = 0.01*var(t',1);
net.divideFcn = 'dividetrain';
[net,tr] = train(net,p,t);
a = net(p)

Searching a matrix by row and column

Lets say I have the matrix
dataSet = [400,300,200,100,200,300,400;
1, 2, 3, 4, 5, 6, 7]
This will give me a 2x7 array with the larger numbers on row 1 and the smaller on row 2.
Lets say I am given the number 200 and asked to find what all the numbers below 200 are. The answer is 3 and 5, because they both correspond to 200, but how can I code this into my script?
>> dataSet(2,dataSet(1,:) == 200)
ans =
3 5
Is this what you want?
[t, ~, u] = unique(dataSet(1,:));
result = accumarray(u, dataSet(2,:).', [], #(x) {x.'});
result = [num2cell(t).' result];
In your example, this gives:
>> result
result =
[100] [ 4]
[200] [1x2 double]
[300] [1x2 double]
[400] [1x2 double]
with
result{2,2} =
3 5
result{3,2} =
2 6
etc

Assigning a value from a cell array to a numerical arrray

How to assign a value from a cell array to a numerical array efficiently? A and B are always square matrices of any size. For simplicity I just allocated small matrices. If for example A{1}=[2 3 8]; then I want allocate value 8 in second row and third column of the B matrix.
E.g.,
Input
A=[1x3 double] [1x3 double] [1x3 double] [1x3 double]
[1x3 double] [1x3 double] [1x3 double] [1x3 double]
[1x3 double] [1x3 double] [1x3 double] [1x3 double]
[1x3 double] [1x3 double] [1x3 double] [1x3 double]
B=zeros(4,4);
A{1}=[2 3 8];
A{2}=[3 4 7];
and so on...
Output
B(2,3)=8;
B(3,4)=7;
and so on...
Use spconvert:
B = full(spconvert(cat(1, A{:})));
You can do this without loops:
B=zeros(4,4);
A{1}=[2 3 8];
A{2}=[3 4 7];
C = cell2mat(A);
C = reshape(C,3,size(C,2)/3)';
indices = sub2ind(size(B), C(:,1), C(:,2));
B(indices) = C(:,3);
For your example this results in:
B =
0 0 0 0
0 0 8 0
0 0 0 7
0 0 0 0
Using sub2ind and indexing
%// creating a sample cell array. Replace this with your actual cell array
A = {[1 2 8] [1 1 4]; [2 1 5] [2 2 1]};
%// Reshaping the cell array to kx3 matrix
Amat = cat(1,A{:}); %// from luis' answer
%// taking first column as row sub, 2nd col as col sub
%// The subs are converted into linear indices to the size of A or B
ind = sub2ind(size(A),Amat(:,1),Amat(:,2));
%// You have done this (helps in fixing the dimensions of B)
B = zeros(size(A));
%// Taking last col of 'Amat' as values and assigning to corresponding indices
B(ind) = Amat(:,3);
Results:
>> B
B =
4 8
5 1
I would suggest looping once over the A cells to find the maximum index values, let's say max_i,max_j.
Then initialize B like:
B=zeros(max_i,max_j)
And loop again over the A cells and assign th values to the corresponding B elements.
Edit: Added example code:
max_i=0
max_j=0
for k=1:size(A,1)
for l=1:size(A,2)
max_i=max([A{k,l}(1),max_i])
max_j=max([A{k,l}(2),max_j])
end
B=zeros(max_i,max_j)
for k=1:size(A,1)
for l=1:size(A,2)
B(A{k,l}(1),A{k,l}(2))=A{k,l}(3)
end

All possible Intersections of cells in cell array : MATLAB.

Consider a cell array ,
H = [ {N1x1} {N2x1} {N3x1} ...{Nmx1} ]
How does get (efficiently) all pairwise intersections of these cells?
Not sure how efficient this will be.
N = numel(H);
[ii jj] = ndgrid(1:N);
result = arrayfun(#(n) intersect(H{ii(n)},H{jj(n)}), 1:N^2, 'uni', 0);
result = reshape(result,N,N);
Example:
H = {[1 2 3], [2 3], [4 5]};
gives
result =
[1x3 double] [1x2 double] [1x0 double]
[1x2 double] [1x2 double] [1x0 double]
[1x0 double] [1x0 double] [1x2 double]
>> result{1,1}
ans =
1 2 3
>> result{1,2}
ans =
2 3
>> result{1,3}
ans =
Empty matrix: 1-by-0
[..]
This also works if H is a multidimensional cell array.
You could also use two for loops. Then you could save half operations explotiing the symmetry of the result.

How to check a specific value in a 1x4 double Cell using Matlab?

If I have a 4x1 Cell structure with :
[1x4 double]
[1x4 double]
[1x4 double]
[1x4 double]
And each Cell has :
[5,3,0,0]
[0,3,5,0]
[1,3,0,0]
[0,3,2,0]
I would like to do the following :
Pick cells that have the first array a value = 5
ans : [5,3,0,0]
Pick cells that have second array a value = 3
ans : [5,3,0,0] [0,3,5,0] [1,3,0,0] [0,3,2,0]
etc
How can I achieve this ?
Code:
a = cell(4,1);
a{1} = [5 3 0 0];
a{2} = [0 3 5 0];
a{3} = [1 3 0 0];
a{4} = [0 3 2 0];
findval = 3;
arrayind = 2;
b = a(cellfun(#(x)x(arrayind) == findval,a));
b{:}
terminal:
ans =
5 3 0 0
ans =
0 3 5 0
ans =
1 3 0 0
ans =
0 3 2 0