let a and b are sequence of k consecutive natural numbers a = (a_i) and b = (b_i). If A an n x n matrix, then A[a,b] (a nontrivial submatrix of A which is obtained from eliminating elements that doesnt belong in rows a and columns b) is called boundary submatrix if
i) a_1 = 1 or a_1 > 1 and A[(a_1 - 1),b] = 0
or
ii) b_1 = 1 or b_1 > 1 and A[a,(b_1 - 1)] = 0
(yes the matrix usually got some zero entries)
what is the easiest code to understand for this case?
I tried to make and array for the sequence using columnk but it seems difficult.
Example for matrix
B =
1 1 1 0
1 2 1 1
0 1 2 2
0 0 3 2
if we pick a = 2,3 and b = 3,4 we get
B(a,b) =
1 1
2 2
and the example of its boundary submatrices of B would be its every principal submatrix, and B([2,3],[2,3]) since B([2,3],1)=0
Consider an n-by-k matrix M and an p-by-1 vector of indexes V ranging from 1 to n. How can I create the p-by-k matrix C where each row corresponds to the entry of M referred to by the value in each row of V.
Example
M = 1 1
1 2
1 3
1 4
and
V = 2
1
3
What I require is the matrix
C = 1 2
1 1
1 3
To assign the rows V of matrix M to a matrix C, you would write:
C = M(V,:);
I have a system of three equations that I'd like to solve via MATLAB, and I'm a bit confused on how to go about doing it.
I have three equations:
A = R*P1
B = R*P2
C = R*P3
A, B, C, and P1, P2, and P3 are 3x1 matrices, while R is a 3x3 matrix. R is the same for all three equations.
I need to find R, and am given A, B, C, and the P's.
I wanted to use fsolve, but it seems that fsolve doesn't work when the variables are matrices. What is an alternative method that you would recommend using?
Just making up some numbers to work with:
P1 = [1;1;1];
P2 = [2;3;4];
P3 = [5;4;3];
R = [2 4 5; 1 5 4; 1 2 3];
Which would mean that:
A = [11;10;6];
B = [36;33;20];
C = [41;37;22];
If A, B, C, P1, P2, P3 are all numerical, why don't you simply use the ldivide or \ operator? This will allow you to solve your linear system directly. I can see that you have the following relationships:
R*P1 = A
R*P2 = B
R*P3 = C
You can see that each matrix equation yields three constraints. What you can do is create one system that encapsulates all matrix equations together thus yielding 9 constraints. As such, you'll need to reformulate this to be able to solve for your coefficients in your matrix R differently. To do this, we need to reshape your matrix R such that it becomes a 9-element vector. In other words, we can then reformulate your system like so:
[P1 0 0 0 0 0 0] [R1] [ ]
[0 0 0 P1 0 0 0] [R2] [ A ]
[0 0 0 0 0 0 P1] [R3] [ ]
[P2 0 0 0 0 0 0] [R4] [ ]
[0 0 0 P2 0 0 0] * [R5] = [ B ]
[0 0 0 0 0 0 P2] [R6] [ ]
[P3 0 0 0 0 0 0] [R7] [ ]
[0 0 0 P3 0 0 0] [R8] [ C ]
[0 0 0 0 0 0 P3] [R9] [ ]
P * R = D
You'll see that we have a 9 x 9 matrix called P, our matrix R which is reshaped into a vector so that we can solve for the coefficients and D are A,B,C concatenated together into one vector. R1 to R9 are the coefficients of the matrix R that are read from left to right and top to bottom.
Therefore, to find your coefficients in your matrix, simply do:
R = P^{-1}*D
As such, simply construct the matrix P and vector D like so:
P = [P1.' zeros(1,6); zeros(1,3) P1.' zeros(1,3); zeros(1,6) P1.'; ...
P2.' zeros(1,6); zeros(1,3) P2.' zeros(1,3); zeros(1,6) P2.'; ...
P3.' zeros(1,6); zeros(1,3) P3.' zeros(1,3); zeros(1,6) P3.'];
D = [A; B; C];
Now, simply solve for R and reshape it back into a 3 x 3 matrix. Therefore:
R = P \ D;
R = reshape(R, 3, 3).';
reshape turns our vector into a 3 x 3 matrix, but it constructs the matrix in column-major format, so you need to transpose the result after you call reshape. With your example, this is what we get. I construct P1, P2, P3, A, B, C, then use the code I had from before:
P1 = [1;1;1];
P2 = [2;3;4];
P3 = [5;4;3];
A = [11;10;6];
B = [36;33;20];
C = [41;37;22];
P = [P1.' zeros(1,6); zeros(1,3) P1.' zeros(1,3); zeros(1,6) P1.'; ...
P2.' zeros(1,6); zeros(1,3) P2.' zeros(1,3); zeros(1,6) P2.'; ...
P3.' zeros(1,6); zeros(1,3) P3.' zeros(1,3); zeros(1,6) P3.'];
D = [A; B; C];
R = P \ D;
R = reshape(R, 3, 3).';
To verify that R is correct, do:
A1 = R*P1;
B1 = R*P2;
C1 = R*P3;
We get for each:
A1 =
11
10
6
B1 =
36
33
20
C1 =
41
37
22
This matches up with your example. However, note that you may get a warning that R is ill-conditioned. This is because you may not have enough constraints to properly find a unique inverse. You may have to add more constraints to get a unique inverse, but if you can't, then use this with caution.
Have you tried to flatten out the matrices inside of the solve function? There is a collect function in MATLAB that collects coefficients, but you need to check if it works with supplying a row matrix, as opposed to a sum. (maybe sum(row matrix) - if it also combines like terms)
Something like:
[x,y,x] = solve(sum(R*P1)==A, sum(R*P2)==B, sum(R*P3)==C, x,y,z)
or the fsolve function
I lack experience in matlab, & have a problem writing a code for a program that starts as follows:
N = input('Enter N:'); % The number of rows (say 6)
M = input('Enter M:'); % The number of columns (say 4)
Mat1 = round(rand(N,M)); % Creating a random binary matrix
B = sum(Mat1); % Calculating the sum of the elements of the columns
Mat2 = sort(B,'descend') % Sorting the elements in a descending order
What I have to do is as follows:
1- Mat1 must have its rows and columns assigned to look like this:
C1 C2 C3 C4
N1 0 1 1 1
N2 0 0 1 1
N3 0 1 1 0
N4 0 1 0 1
N5 1 1 0 1
N6 1 0 0 1
2- Mat2 should display the assigned labels of the columns instead of the sum values to be like:
Mat2 = [C4 C2 C3 C1] % The labels of the columns
in addition to
Mat2 = [5 4 3 2] % The sorting of the columns in a descending order
3- Mat1 must be divided into 2XN subsets; for example:
C1 C2 C3 C4
N1 0 1 1 1
N2 0 0 1 1
4- Out of each subset, make a list which contains the columns that have only 1 in it, such as:
N1N2 = [C3 C4] % Ascending sorting is done according to the values of Mat2
Hope the explanation is clear enough. Thank you for any sort of help you might be able to provide.
Im looking to create a matrix of rank k.
The dimension of the matrix is m x n. The input k satisfies that condition that k < min(m,n).
It's not really so clear what you are aiming for.
But in order to create a matrix B with specific rank k, from a matrix A (with rank at least k), you may like to utilize svd and proceed like:
>>> A= rand(7, 5);
>>> rank(A)
ans = 5
>>> [U, S, V]= svd(A);
>>> k= 3;
>>> B= U(:, 1: k)* S(1: k, 1: k)* V(:, 1: k)';
>>> rank(B)
ans = 3
Well, a trivial method is to produce a matrix that looks like:
1 0 0 0 0
0 1 0 0 0
0 0 1 1 1
0 0 0 0 0
i.e. k columns of the identity matrix, then repeat the last column n-k times (or m-k times, depending on orientation).
A matrix of rank 1 can be created by the outer product of two vectors, for example:
A = randn(10,1) * randn(1,10);
Add together k of these and you will have a matrix of rank k. Like this:
>> A = zeros(10);
>> for i = 1:4, A = A + randn(10,1) * randn(1,10); end
>> rank(A)
ans = 4