Accessing a matrix in Matlab - user-interface

Assume user input data as below. I define my matrix is cost. The matrix i created is 3 by 3 matrix. So the matrix should form like this:
cost = [c11 c12 c13
c21 c22 c23
c31 c32 c33]
Since i want to display set of row, i do it like this :
c1 = cost(1,:); % it will become c1 = c11 c12 c13
c2 = cost(2,:); % it will become c2 = c21 c22 c23
c3 = cost(3,:); % it will become c3 = c31 c32 c33
Then i want the value in the matrix. I do it like this.
c11 = cost(1,1);
c12 = cost(1,2);
c13 = cost(1,3);
c21 = cost(2,1);
c22 = cost(2,2);
c23 = cost(2,3);
c31 = cost(3,1);
c32 = cost(3,2);
c33 = cost(3,3);
So this is the equation that i want to use for this type of matrix.
lambda =
((8*c13*c23*c33*Pdt)+(4*c12*c23*c33)+(4*c13*c22*c33)+(4*c13*c23*c32)) ./ (4*c23*c33)+(4*c13*c33)+(4*c13*c23));
So my problem is, if i want to make 4 by 3 matrix, and it would generate a matrix like this:
cost = [c11 c12 c13
c21 c22 c23
c31 c32 c33
c41 c42 c43]
The equation that i want to use for this matrix(4 by 3) is quite different. So how im gonna do it? Do i need to use if else statement? or do while? Can anyone help me solve this? Can anyone create the code?

Why do you explicitly create the variables c11, c12, ...? Surely it would be easier to just access the matrix in your equation like this:
lambda =
((8*cost(1,3)*cost(2,3)*cost(3,3)*Pdt)+(4*cost(1,2)*cost(2,3)*cost(3,3)+(4*cost(1,3)*cost(2,2)*c(3,3))+(4*cost(1,3)*cost(2,3)*cost(3,2)) ./
(4*cost(2,3)*cost(3,3))+(4*cost(1,3)*cost(3,3))+(4*cost(1,3)*cost(2,3)));
For your question, yes, just use a simple if statment, like this:
if size(cost,1) == 3
%equation for matrix size 3x3
else
%equation for matriz size 4x3

Related

Avoid accuracy problems while computing the permanent using the Ryser formula

Task
I want to calculate the permanent P of a NxN matrix for N up to 100. I can make use of the fact that the matrix features only M=4 (or slightly more) different rows and cols. The matrix might look like
A1 ... A1 B1 ... B1 C1 ... C1 D1 ... D1 |
... | r1 identical rows
A1 ... A1 B1 ... B1 C1 ... C1 D1 ... D1 |
A2 ... A2 B2 ... B2 C2 ... C2 D2 ... D2
...
A2 ... A2 B2 ... B2 C2 ... C2 D2 ... D2
A3 ... A3 B3 ... B2 C2 ... C2 D2 ... D2
...
A3 ... A3 B3 ... B3 C3 ... C3 D3 ... D3
A4 ... A4 B4 ... B4 C4 ... C4 D4 ... D4
...
A4 ... A4 B4 ... B4 C4 ... C4 D4 ... D4
---------
c1 identical cols
and c and r are the multiplicities of cols and rows. All values in the matrix are laying between 0 and 1 and are encoded as double precision floating-point numbers.
Algorithm
I tried to use the Ryser formula to calculate the permanent. For the formula, one needs to first calculate the sum of each row and multiply all the row sums. For the matrix above this yields
S0 = (c1 * A1 + c2 * B1 + c3 * C1 + c4 * D1)^r1 * ...
* (c1 * A4 + c2 * B4 + c3 * C4 + c4 * D4)^r4
As a next step the same is done with col 1 deleted
S1 = ((c1-1) * A1 + c2 * B1 + c3 * C1 + c4 * D1)^r1 * ...
* ((c1-1) * A4 + c2 * B4 + c3 * C4 + c4 * D4)^r4
and this number is subtracted from S0.
The algorithm continues with all possible ways to delete single and group of cols and the products of the row sums of the remaining matrix are added (even number of cols deleted) and subtracted (odd number of cols deleted).
The task can be solved relative efficiently if one makes use of the identical cols (for example the result S1 will pop up exactly c1 times).
Problem
Even if the final result is small the values of the intermediate results S0, S1, ... can reach values up to N^N. A double can hold this number but the absolute precision for such big numbers is below or on the order of the expected overall result. The expected result P is on the order of c1!*c2!*c3!*c4! (actually I am interested in P/(c1!*c2!*c3!*c4!) which should lay between 0 and 1).
I tried to arrange the additions and subtractions of the values S in a way that the sums of the intermediate results are around 0. This helps in the sense that I can avoid intermediate results that are exceeding N^N, but this improves things only a little bit. I also thought about using logarithms for the intermediate results to keep the absolute numbers down - but the relative accuracy of the encoded numbers will be still bounded by the encoding as floating point number and I think I will run into the same problem. If possible, I want to avoid the usage of data types that are implementing a variable-precision arithmetic for performance reasons (currently I am using matlab).

Pulling information from two URIs using Hadoop

Let's say I've got one giant sparse matrix in HDFS at matrixX and another at matrixY and I want to do a giant matrix multiplication and write to matrixY. Can I draw from two different URIs in Hadoop? How do I do this? All the examples I've seen have one input directory and one output directory.
You can read from multiple sources as below:
MultipleInputs.addInputPath(jobConf, MultipleInputs.addInputPath(jobConf,
new Path(MatixX),
TextInputFormat.class,
ColumnReaderMapper.class);
MultipleInputs.addInputPath(jobConf,
new Path(MatirxY),
TextInputFormat.class,
RowReaderMapper.class);
Lets say you have to matrix A and B:
a11 a12 a13
a21 a22 a23
a31 a32 a33
b11 b12 b13
b21 b22 b23
b31 b32 b33
A * B = summation (row of a * column of b)
How about storing Matrix A as row major and matrix b as column major. Now you can set keys as row number in RowReader and Column Number in Columnreader in next mapper and send it to the same reducer which will do the summation and write the output. You can do many other optimizations but that should be the first start.

Determine elements of a matrix when sum of rows and columns are given

There is 4x4 matrix with all 4 diagonal elements zero. All other elements are non negative integers. Sum of all 4 rows and 4 columns are known individually. Is it possible to determine the remaining 12 elements of the matrix? Eg
0 1 1 0 sum=2
2 0 0 1 sum=3
4 1 0 0 sum=5
0 1 6 0 sum=7
sum=6 sum=3 sum=7 sum=1
Any guidance will be very helpful.
Thanks
The matrix is
0 a12 a13 a14
a21 0 a23 a24
a31 a32 0 a34
a41 a42 a43 0
The problem is to solve a set of linear equations:
a12 + a13 + a14 = c1
a21 + a23 + a24 = c2
and so on. We have 12 variables and 8 equations (4 for the rows and 4 for the columns). To solve a linear equation system in 12 variables, we generally need 12 equations. Since the number of equations is lesser, the system will not have a unique solution. It may have infinitely many solutions.
The matrix is
0 a12 a13 a14
a21 0 a23 a24
a31 a32 0 a34
a41 a42 a43 0
The problem is to solve a set of linear equations:
a12 + a13 + a14 = r1
a21 + a23 + a24 = r2
a31 + a32 + a34 = r3
a41 + a43 + a44 = r4
a21 + a31 + a41 = c1
a12 + a32 + a42 = c2
a13 + a23 + a43 = c3
a14 + a34 + a44 = c4
Thus you need to solve an equation of the form Ax = b with A consisting of only 0 and 1 coefficients. Use Gauss Elimination and Euclidian Algorithm to find integer Matrices S, D, T such that D is in Diagonal form and SDT = A. If you do not know how to do this search the web for Smith normal form algorithm.
Then
SDTx = Ax = b
Thus
DTx = S-1Ax = S-1b
Since D is in diagonal form you can check if you can solve
Dy = S-1b
for y. You also find a base for the (Homogenous) solution space. This in turn can then be used to cut down the complexity in the search for the positive solutions of the original equation.

Divide and Conquer Matrix Multiplication

I am having trouble getting divide and conquer matrix multiplication to work. From what I understand, you split the matrices of size nxn into quadrants (each quadrant is n/2) and then you do:
C11 = A11⋅ B11 + A12 ⋅ B21
C12 = A11⋅ B12 + A12 ⋅ B22
C21 = A21 ⋅ B11 + A22 ⋅ B21
C22 = A21 ⋅ B12 + A22 ⋅ B22
My output for divide and conquer is really large and I'm having trouble figuring out the problem as I am not very good with recursion.
example output:
Original Matrix A:
4 0 4 3
5 4 0 4
4 0 4 0
4 1 1 1
A x A
Classical:
44 3 35 15
56 20 24 35
32 0 32 12
29 5 21 17
Divide and Conquer:
992 24 632 408
1600 272 720 1232
512 0 512 384
460 17 405 497
Could someone tell me what I am doing wrong for divide and conquer? All my matrices are int[][] and classical method is the traditional 3 for loop matrix multiplication
You are recursively calling divideAndConquer in the wrong way. What your function does is square a matrix. In order for divide and conquer matrix multiplication to work, it needs to be able to multiply two potentially different matrixes together.
It should look something like this:
private static int[][] divideAndConquer(int[][] matrixA, int[][] matrixB){
if (matrixA.length == 2){
//calculate and return base case
}
else {
//make a11, b11, a12, b12 etc. by dividing a and b into quarters
int[][] c11 = addMatrix(divideAndConquer(a11,b11),divideAndConquer(a12,b21));
int[][] c12 = addMatrix(divideAndConquer(a11,b12),divideAndConquer(a12,b22));
int[][] c21 = addMatrix(divideAndConquer(a21,b11),divideAndConquer(a22,b21));
int[][] c22 = addMatrix(divideAndConquer(a21,b12),divideAndConquer(a22,b22));
//combine result quarters into one result matrix and return
}
}
Some debugging approaches to try:
Try some very simple test matrices as input (e.g. all zeros, with a one or a few strategic ones). You may see a pattern in the "failures" that will show you where your error(s) are.
Make sure your "classical" approach is giving you correct answers. For small matrices, you can use Woflram Alpha on-line to test answers: http://www.wolframalpha.com/examples/Matrices.html
To debug recursion: add printf() statements at the entry and exit of your function, including the invocation arguments. Run your test matrix, write the output to a log file, and open the log file with a text editor. Step through each case, writing your notes alongside in the editor making sure it's working correctly at each step. Add more printf() statements and run again if needed.
Good luck with the homework!
Could someone tell me what I am doing wrong for divide and conquer?
Yes:
int[][] a = divideAndConquer(topLeft);
int[][] b = divideAndConquer(topRight);
int[][] c = divideAndConquer(bottomLeft);
int[][] d = divideAndConquer(bottomRight);
int[][] c11 = addMatrix(classical(a,a),classical(b,c));
int[][] c12 = addMatrix(classical(a,b),classical(b,d));
int[][] c21 = addMatrix(classical(c,a),classical(d,c));
int[][] c22 = addMatrix(classical(c,b),classical(d,d));
You are going through an extra multiplication step here: you shouldn't be calling both divideAndConquer() and classical().
What you are effectively doing is:
C11 = (A11^2)⋅(B11^2) + (A12^2)⋅(B21^2)
C12 = (A11^2)⋅(B12^2) + (A12^2)⋅(B22^2)
C21 = (A21^2)⋅(B11^2) + (A22^2)⋅(B21^2)
C22 = (A21^2)⋅(B12^2) + (A22^2)⋅(B22^2)
which is not correct.
First, remove the divideAndConquer() calls, and replace a/b/c/d by topLeft/topRight/etc.
See if it gives you the proper results.
Your divideAndConquer() method needs a pair of input parameters, so you can use A*B. Once you get that working, get rid of the calls to classical(), and use divideAndConquer() instead. (or save them for matrices that are not a multiple of 2 in length.)
You might find the Wiki article on Strassen's algorithm helpful.

Projective transformation fitting

Given set of points in 3D ( X = (x1, x2, x3), Y = (y1, y2, y3) ), how can I fit transformation from X to Y?
As far as I know this is called projective transformation.
Here is example of X and Y.
Blue and red lines in X are parallel, but they are not parallel in Y.
Projective transformations in 3d have an associated 4x4 matrix (modulo a constant multiplication). You can find the matrix with least square fitting.
Well. I found some useful information:
This transformation is non-linear and it is not possible to represent non-linear transformation with a matrix. There are some tricks such as using homogenous coordinates. but it doesn't make all non-linear transformations representable using matrices.
However, approximating a nonlinear function by a linear function is possible.
So, the task is to find best fitting linear transformation, right?
There is a simple solution using linear regression.
Say the transformation matrix is named A and has dimensions 3x3. And say you have N vectors (points) in 3D before and after the transformation - so you have matrices X and Y of 3 rows and N columns. Then the transformation is:
Y = A X + B
where B is a vector of length 3 and specifies the shift. You can rewrite the matrix multiplication using indices:
y[i,j] = sum(k=1..3)(a[i,k] * x[k,j]) + b[i]
for i = 1..3 and j = 1 .. N. So, you have 12 unknown variables (a, b), and 3 * N equations. For N >= 4, you simply find the best solution using linear regression.
For example, in R it is very easy:
# input data
X = matrix(c(c(0, 0, 0), c(1, 0, 0), c(0, 1, 0), c(0, 1, 1)), nrow = 3)
Y = matrix(c(c(1, 0, 1), c(2, 0, 1), c(1, 1, 1), c(1, 1, 2)), nrow = 3)
# expected transformation: A is identity matrix, b is [1, 0, 1]
N = dim(Y)[2]
# transform data for regression
a1 = rbind(t(X), matrix(rep(0, 3*2*N), ncol = 3))
a2 = rbind(matrix(rep(0, 3*N), ncol = 3), t(X), matrix(rep(0, 3*N), ncol = 3))
a3 = rbind(matrix(rep(0, 3*2*N), ncol = 3), t(X))
b1 = rep(1:0, c(N, 2*N))
b2 = rep(c(0, 1, 0), each = N)
b3 = rep(0:1, c(2*N, N))
y = as.vector(t(Y))
# do the regression
summary(lm(y ~ 0 + a1 + a2 + a3 + b1 + b2 + b3))
And the output is:
[...]
Coefficients:
Estimate Std. Error t value Pr(>|t|)
a11 1.000e+00 NA NA NA
a12 -2.220e-16 NA NA NA
a13 -3.612e-32 NA NA NA
a21 7.850e-17 NA NA NA
a22 1.000e+00 NA NA NA
a23 -1.743e-32 NA NA NA
a31 0.000e+00 NA NA NA
a32 0.000e+00 NA NA NA
a33 1.000e+00 NA NA NA
b1 1.000e+00 NA NA NA
b2 -7.850e-17 NA NA NA
b3 1.000e+00 NA NA NA
Residual standard error: NaN on 0 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: NaN
F-statistic: NaN on 12 and 0 DF, p-value: NA
as expected.

Resources