Writing a MATLAB code using an algorithm for a reciprocal matrix - matlab

I want to write a code for the following algorithm using MATLAB.
**Input:** An square reciprocal matrix T = (tij) for (i, j = 1, 2, . . . , n),
**Output:** A square reciprocal matrix C = (cij) for (i, j = 1, 2, . . . , n);
1: for i = 1; i < n; i ++ do
2: for j = i ; j < n; j ++ do
3: cij = sqrt(tij /tji );
4: cji = 1/cij ;
5: end for
6: end for
I have the following matrix T:
T=[...
0.08 0.02 0.34 0.67;...
0.01 0.08 0.17 0.34;...
0.02 0.04 0.09 0.18;...
0.01 0.02 0.04 0.09]
The answer C I found on a paper is:
C = [...
1 2 4 8;...
0.50 1 2 4;...
0.25 0.50 1 2;...
0.13 0.25 0.50 1]
So far, I have tried the following code, but I am not certain about it. I couldn't find the exact answer C above. Any idea or help, please?
C=zeros(n,n);
for i = 1:n
for j = i:n
C(i,j) = sqrt(T(i,j)/T(j,i));
C(j,i) = 1/C(i,j) ;
end
end
C;

Related

Matlab multiple matrix rowwise based on rule

I have this matrix
mp=
2
5
8
fp=
0.67 0.34 0
0.34 0.34 0.34
0 0.5 0.5
and this matrix
t=
1
1
1
2
3
2
3
with this rule:
if t=1 then the output = mp*(fp first row)
if t=2 then the output = mp*(fp 2nd row)
if t=3 then the output = mp*(fp 3rd row)
so the output should be like this:
output=
3
3
3
5
6,5
5
6,5
im trying using this code
but the output==t
[o p]=size(t)
[q r]=size(mp)
for i=1:o;
j=1:q;
if t(i)==j
output=mp*fp(j,:)
else
output=t(i)
end
end
One line Solution
you can use the following syntax:
output = sum(repmat(mp,1,length(t))'.*fp(t,:),2)
result
ans =
3.0400
3.0400
3.0400
5.1000
6.5000
5.1000
6.5000
example
%variables declaration
fp= [0.67 0.34 0; 0.34 0.34 0.34;0 0.5 0.5];
mp = [2; 5; 8]
t = [1,1,1,2,3,2,3]
%calculates result
output = sum(repmat(mp,1,length(t))'.*fp(t,:),2)
As your t values are already row numbers, you can directly use t values for multiplication.
mp=[2
5
8] ;
fp=[0.67 0.34 0
0.34 0.34 0.34
0 0.5 0.5] ;
t=[1
1
1
2
3
2
3] ;
iwant = cell(size(t)) ; % initilaize the required data
for i = 1:length(t) % loop for each value of _t_
iwant{i} = mp*fp(t(i),:) ;
end

Copy element of matrix to a vector MATLAB

Assume we have a matrix A (2x5), with the first row containing the numbers:
1 2 3 5 7
and the second row:
0.4 0.1 0.2 0.1 0.2
Also, there is a 10 dimensional vector B with numbers 1,2,3...10.
How can I create a new 10 dimensional vector C that will only contain the values of A (second row) when A(1,:) == B, else 0.
So the new vector C should have the form:
0.4 0.1 0.2 0 0.1 0 0.2 0 0 0
(add zero for the cells of B that are not in A).
I tried this solution but I have a problem due to the difference in dimensions between A and B.
for i=1:53
if B(i) == A(1,i)
C{1,i} = A(2,i);
else
C{1,i}=0;
end
end
Index exceeds matrix dimensions.
It's not very clear what you're after, but this at least gives the desired output:
A = [1 2 3 5 7; 0.4 0.1 0.2 0.1 0.2];
B = 1:10;
[tf,loc] = ismember(A(1,:), B);
C = zeros(1,10);
C(loc(tf)) = A(2,tf)
[I'm assuming you mean 10 element vector, rather than 10 dimensional...]
If you just want to use the first row of A as indices and the second row as assigned values, then you don't need to use B at all and you can do something like this:
A = [1 2 3 5 7; 0.4 0.1 0.2 0.1 0.2];
C = zeros(1,10);
C(A(1,:)) = A(2,:)
How about removing the for loop and do it inline using ismember function:
A = [1 2 3 5 7; 0.4 0.1 0.2 0.1 0.2];
B = 1:10;
C = zeros(1,10);
C(B(ismember(B, A(1,:)))) = A(2,ismember(A(1,:),B));
Hint: Even if we happen to have a value in A(1,:) which B does not have, this solution will work.
Using ismember and a for loop:
clc; clear;
A=[
1 2 3 5 7;
0.4 0.1 0.2 0.1 0.2
];
B = 1:10;
C = zeros(1,10);
for j = 1:10
if ismember(j, A(1,:))
C(j) = A(2, A(1,:) == j);
else
C(j) = 0;
end
end
C

XOR with Neural Networks (Matlab)

So, I'm hoping this is a real dumb thing I'm doing, and there's an easy answer. I'm trying to train a 2x3x1 neural network to do the XOR problem. It wasn't working, so I decided to dig in to see what was happening. Finally, I decided to assign the weights my self. This was the weight vector I came up with:
theta1 = [11 0 -5; 0 12 -7;18 17 -20];
theta2 = [14 13 -28 -6];
(In Matlab notation). I deliberately tried to make no two weights be the same (barring the zeros)
And, my code, really simple in matlab is
function layer2 = xornn(iters)
if nargin < 1
iters = 50
end
function s = sigmoid(X)
s = 1.0 ./ (1.0 + exp(-X));
end
T = [0 1 1 0];
X = [0 0 1 1; 0 1 0 1; 1 1 1 1];
theta1 = [11 0 -5; 0 12 -7;18 17 -20];
theta2 = [14 13 -28 -6];
for i = [1:iters]
layer1 = [sigmoid(theta1 * X); 1 1 1 1];
layer2 = sigmoid(theta2 * layer1)
delta2 = T - layer2;
delta1 = layer1 .* (1-layer1) .* (theta2' * delta2);
% remove the bias from delta 1. There's no real point in a delta on the bias.
delta1 = delta1(1:3,:);
theta2d = delta2 * layer1';
theta1d = delta1 * X';
theta1 = theta1 - 0.1 * theta1d;
theta2 = theta2 - 0.1 * theta2d;
end
end
I believe that's right. I tested various parameters (of the thetas) with the finite differences method to see if they were right, and they seemed to be.
But, when I run it, it eventually just all boils down to returning all zeros. If I do xornn(1) (for 1 iteration) I get
0.0027 0.9966 0.9904 0.0008
But, if I do xornn(35)
0.0026 0.9949 0.9572 0.0007
(It's started a descent in the wrong direction) and by the time I get to xornn(45) I get
0.0018 0.0975 0.0000 0.0003
If I run it for 10,000 iterations, it just returns all 0's.
What is going on? Must I add regularization? I would have thought such a simple network wouldn't need it. But, regardless, why does it move away from an obvious good solution that I have hand fed it?
Thanks!
AAARRGGHHH! The solution was simply a matter of changing
theta1 = theta1 - 0.1 * theta1d;
theta2 = theta2 - 0.1 * theta2d;
to
theta1 = theta1 + 0.1 * theta1d;
theta2 = theta2 + 0.1 * theta2d;
sigh
Now tho, I need to figure out how I'm computing the negative derivative somehow when what I thought I was computing was the ... Never mind. I'll post here anyway, just in case it helps someone else.
So, z = is the sum of inputs to the sigmoid, and y is the output of the sigmoid.
C = -(T * Log[y] + (1-T) * Log[(1-y))
dC/dy = -((T/y) - (1-T)/(1-y))
= -((T(1-y)-y(1-T))/(y(1-y)))
= -((T-Ty-y+Ty)/(y(1-y)))
= -((T-y)/(y(1-y)))
= ((y-T)/(y(1-y))) # This is the source of all my woes.
dy/dz = y(1-y)
dC/dz = ((y-T)/(y(1-y))) * y(1-y)
= (y-T)
So, the problem, is that I accidentally was computing T-y, because I forgot about the negative sign in front of the cost function. Then, I was subtracting what I thought was the gradient, but was in fact the negative gradient. And, there. That's the problem.
Once I did that:
function layer2 = xornn(iters)
if nargin < 1
iters = 50
end
function s = sigmoid(X)
s = 1.0 ./ (1.0 + exp(-X));
end
T = [0 1 1 0];
X = [0 0 1 1; 0 1 0 1; 1 1 1 1];
theta1 = [11 0 -5; 0 12 -7;18 17 -20];
theta2 = [14 13 -28 -6];
for i = [1:iters]
layer1 = [sigmoid(theta1 * X); 1 1 1 1];
layer2 = sigmoid(theta2 * layer1)
delta2 = T - layer2;
delta1 = layer1 .* (1-layer1) .* (theta2' * delta2);
% remove the bias from delta 1. There's no real point in a delta on the bias.
delta1 = delta1(1:3,:);
theta2d = delta2 * layer1';
theta1d = delta1 * X';
theta1 = theta1 + 0.1 * theta1d;
theta2 = theta2 + 0.1 * theta2d;
end
end
xornn(50) returns 0.0028 0.9972 0.9948 0.0009 and
xornn(10000) returns 0.0016 0.9989 0.9993 0.0005
Phew! Maybe this will help someone else in debugging their version..

I have a bug in a MATLAB code and I cannot find it

I am trying to make a simple linear programming optimization, using MATLAB R2012b. I came up with this code:
clear;
y=1663;
f=[100; 100; 100; 100; 100; 100; 100; 100];
A=[1/867.2 1/704.7 1/619.2 1/679 1/770.3 1/658.5 1/749 1/783.1
-105.2 -74.4 -94.3 -88.2 -108.9 999 -112.7 -91.4
-94.1 -71.2 -87.6 -87.8 -99.4 999 -100 -81.2
0.5 0.6 4.5 3 28 15 4.5 12.5
0.78 2.11 0.01 0.01 0.56 0.09 0.02 0.9
93.63 16.3 0.01 0.01 1.44 0.09 0.03 37.64
0.82 3.23 0.03 0.36 3.13 29.97 0.04 16.16];
b=[y/0.775; -95*y; -85*y; 10*y; 1*y; 35*y; 18*y];
Aeq=[1; 1; 1; 1; 1; 1; 1; 1];
beq=y;
lb=zeros(8,1);
hb=[550; 250; 50; 200; 50; 350; 400];
linprog(f, A, b, Aeq, beq, lb, hb);
When I try to run this source, it says that The number of rows in A must be the same as the number of elements of b.
A has 7 rows and b has 7 elements, as it can be seen. I cannot see the mistake. Where is it?
Thanks.
You have to redefine Aeq. Since you have only one equality constraint, it should be 1xn. I also included x as an output of linprog.
Aeq=[1, 1, 1, 1, 1, 1, 1, 1];
x=linprog(f, A, b, Aeq, beq, lb, hb);
The result is
x =
200.7466
59.3486
30.1325
62.8837
35.7733
1.6669
341.6570
930.7914

Remove any column with same values in MATLAB

I need to remove any column in a matrix which has same values in it. I designed it using a for-loop in MATLAB. I wanted to know if a better/faster way exists using vectorization.
mat = [ 0.56 0.2 1 0 45; 0.566 0.2 4 0 45; 0.52 0.2 6 0 45; 0.56 0.2 6 0 41 ];
[row col] = size(mat) ;
bitmat = true(1,col) ;
for i = 2:row, tf = (mat(i-1,:) == mat(i,:)) ; bitmat = bitmat & tf ; end
mat(:,bitmat) = [] ;
Thanks!
Here's a simple one-liner using the functions DIFF and ANY:
mat = mat(:,any(diff(mat,1)));