Matlab, 1 x M matrix from N x M matrix [duplicate] - matlab

This question already has answers here:
How to subtract a vector from each row of a matrix? [duplicate]
(2 answers)
Closed 7 years ago.
I'm trying to substract a 1 x M matrix from a N x M matrix.
lets say my 1 x M matrix is [1 2]
and my N x M matrix is [3 4; 5 4; 1 6]
and what I want as a result is [2 2; 4 2; 0 4]
I know how to do this with a for loop etc, what I'm trying to figure out is is there a math way of doing this in a single line?
Thanks.

You can use the repmat function to extend your 1xM matrix to NxM and then perform the subtraction.
>> M = [1 2];
>> N = [3 4; 5 4; 1 6];
>> N - repmat(M, length(N), 1)
ans =
2 2
4 2
0 4
Alternatively as pointed out by Divakar you can use
>> bsxfun(#minus, N, M)
ans =
2 2
4 2
0 4

Related

Turning matrix diagonals to columns

I am looking for a matrix operation of the form: B = M*A*N where A is some general square matrix and M and N are the matrices I want to find.
Such that the columns of B are the diagonals of A. The first column the main diagonal, the second the diagonal shifted by 1 from the main and so on.
e.g. In MATLAB syntax:
A = [1, 2, 3
4, 5, 6
7, 8, 9]
and
B = [1, 2, 3
5, 6, 4
9, 7, 8]
Edit:
It seems a pure linear algebra solution doesn't exist. So I'll be more precise about what I was trying to do:
For some vector v of size 1 x m. Then define C = repmat(v,m,1). My matrix is A = C-C.';.
Therefore, A is essentially all differences of values in v but I'm only interested in the difference up to some distance between values.
Those are the diagonals of A; but m is so large that the construction of such m x m matrices causes out-of-memory issues.
I'm looking for a way to extract those diagonals in a way that is as efficient as possible (in MATLAB).
Thanks!
If you're not actually looking for a linear algebra solution, then I would argue that constructing three additional matrices the same size as A using two matrix multiplications is very inefficient in both time and space complexity. I'm not sure it's even possible to find a matrix solution, given my limited knowledge of linear algebra, but even if it is it's sure to be messy.
Since you say you only need the values along some diagonals, I'd construct only those diagonals using diag:
A = [1 2 3;
4 5 6;
7 8 9];
m = size(A, 1); % assume A is square
k = 1; % let's get the k'th diagonal
kdiag = [diag(A, k); diag(A, k-m)];
kdiag =
2
6
7
Diagonal 0 is the main diagonal, diagonal m-1 (for an mxm matrix) is the last. So if you wanted all of B you could easily loop:
B = zeros(size(A));
for k = 0:m-1
B(:,k+1) = [diag(A, k); diag(A, k-m)];
end
B =
1 2 3
5 6 4
9 7 8
From the comments:
For v some vector of size 1xm. Then B=repmat(v,m,1). My matrix is A=B-B.'; A is essentially all differences of values in v but I'm only interested in the difference up to some distance between values.
Let's say
m = 4;
v = [1 3 7 11];
If you construct the entire matrix,
B = repmat(v, m, 1);
A = B - B.';
A =
0 2 6 10
-2 0 4 8
-6 -4 0 4
-10 -8 -4 0
The main diagonal is zeros, so that's not very interesting. The next diagonal, which I'll call k = 1 is
[2 4 4 -10].'
You can construct this diagonal without constructing A or even B by shifting the elements of v:
k = 1;
diag1 = circshift(v, m-k, 2) - v;
diag1 =
2 4 4 -10
The main diagonal is given by k = 0, the last diagonal by k = m-1.
You can do this using the function toeplitz to create column indices for the reshuffling, then convert those to a linear index to use for reordering A, like so:
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> n = size(A, 1);
>> index = repmat((1:n).', 1, n)+n*(toeplitz([1 n:-1:2], 1:n)-1);
>> B = zeros(n);
>> B(index) = A
B =
1 2 3
5 6 4
9 7 8
This will generalize to any size square matrix A.

Matrix to Diagonal Matrix in MATLAB [duplicate]

This question already has answers here:
How to vectorize row-wise diagonalization of a matrix
(4 answers)
Closed 9 years ago.
Let's say I have a matrix in MATLAB like
A = [1 2 3;
4 5 6;
7 8 9]
and I would like to obtain a matrix of the form
B = [1 0 0;
0 4 0;
0 0 7;
2 0 0;
0 5 0;
0 0 8;
3 0 0;
0 6 0;
0 0 9]
i.e. a matrix that is a concatenation of three diagonal matrices, with each having the columns of matrix A at their diagonals. I know how to do this using a for loop over the columns of A and then concatenating all the results but I am looking for a shorter way to do this. Please share your ideas.
B(repmat(eye(3),3,1)==1) = A;
reshape(B, [], 3)
Here's a way using linear indexing:
B(sub2ind([9 3], 1:9, mod(0:8,3)+1))=A;
reshape(B,9,3)
If you want this to be generic, realize that each column of the original becomes a diagonal. Therefore, the number of rows in the original becomes the number of columns in the output, and 3 rows x cols becomes the number of rows. The rest of the answer doesn't change at all:
c = size(A,1);
r = size(A,1) * size(A,2); #% or prod(size(A));
B(sub2ind([r c], 1:r, mod(0:(r-1),c)+1)) = A;
B = sparse( 1:numel(A), repmat( 1:size(A,2), [1 size(A,1)] ),...
A(:), numel(A), size(A,2));
should do the trick.
You can B = full(B); if you want a full matrix

How to perform this kind of matrix division in Matlab?

This problem is probably less to do with Matlab and more to do with matrix algebra (which I mostly forget from my college courses). Say I have a m x n matrix X and a m x 1 matrix B. How would I divide the X by B such that all the elements of the ith row of X are piecewise divided by the ith row of B, resulting in another m x n matrix Y?
E.g.
X = [2 4 8; 3 9 27; 4 16 64]
B = [2; 3; 4]
X ? B = [2/2 4/2 8/2; 3/3 9/3 27/3; 4/4 16/4 64/4]
ans =
1 2 4
1 3 9
1 4 16
Better not use repmat - it is slow and allocates additional memory for the workspace. You can use bsxfun, which is an inbuilt function, so it is faster and avoids the extra workspace:
X = [2 4 8; 3 9 27; 4 16 64]
B = [2; 3; 4]
bsxfun(#rdivide, X, B)
ans =
1 2 4
1 3 9
1 4 16
Junuxx's comment pointed me in the right direction. The solution I used to get what I wanted is:
B_prime = repmat(B,1,3)
X ./ B_prime
ans =
1 2 4
1 3 9
1 4 16
I'd still like to know what this kind of operation is called (if it even has a formal name).
X is m x n and B is m x 1
size(X,2) gives the value of n i.e. number of columns
So, you need to do:
X./repmat(B,1,size(X,2))
X = [2 4 8; 3 9 27; 4 16 64]
B = [2; 3; 4]
Result= X./B(:,ones(1,3)) %is faster then repmat
Result =
1 2 4
1 3 9
1 4 16

How can I remove the row of an mx3 matrix from an nx3 matrix (n>m)?

In matlab, If an m by 3 matrix has rows that all exist in a bigger n by 3 matrix, how can I create a (n-m) by 3 matrix that does not contain the rows of the first (m by 3) matrix?
e.g. if the first matrix is [1 4 6] and the second matrix [1 2 3; 1 4 6; 8 7 4], how can I come up with the matrix: [1 2 3;8 7 4]?
That's a job for ismember with the 'rows' option:
a = [1 4 6];
b = [1 2 3; 1 4 6; 8 7 4];
eq_rows = ismember(b,a,'rows');
result = b(~eq_rows,:)

What does A=[x; y'] in Matlab mean?

I'm learning Matlab and I see a line that I don't understand:
A=[x; y']
What does it mean? ' usually means the transponate but I don't know what ; means in the vector. Can you help me?
The [ ] indicates create a matrix.
The ; indicates that the first vector is on the first line, and that the second one is on the second line.
The ' indicates the transponate.
Exemple :
>> x = [1,2,3,4]
x =
1 2 3 4
>> y = [5;6;7;8]
y =
5
6
7
8
>> y'
ans =
5 6 7 8
>> A = [x;y']
A =
1 2 3 4
5 6 7 8
[x y] means horizontal cat of the vectors, while [x;y] means vertical.
For example (Horizontal cat):
x = [1
2
3];
y = [4
5
6];
[x y] = [1 4
2 5
3 6];
(Vertical cat):
x = [1 2 3];
y = [4 5 6];
[x; y] =
[1 2 3;
4 5 6];
Just to be clear, in MATLAB ' is the complex conjugate transpose. If you want the non-conjugating transpose, you should use .'.
It indicates the end of a row when creating a matrix from other matrices.
For example
X = [1 2];
Y = [3,4]';
A = [X; Y']
gives a matrix
A = [ 1 2 ]
[ 3 4 ]
This is called vertical concatenation which basically means forming a matrix in row by row fashion from other matrices (like the example above). And yes you are right about ' indicating the transpose operator. As another example you could use it to create a transposed vector as follows
Y = [1 2 3 4 5];
X = [1; 2; 3; 4; 5];
Y = Y';
Comparing the above you will see that X is now equal to Y. Hope this helps.
Let set the size of x m*n (m rows and n columns) and the size of y n*p.
Then A is the matrix formed by the vertical concatenation of x and the transpose of y (operator '), and its size is (m+p)*n. The horizontal concatenation is done with comma instead of semi-column.
This notation is a nice shorthand for function vertcat.
See http://www.mathworks.fr/help/techdoc/math/f1-84864.html for more information
The semicolon ' ; ' is used to start a new row.
e.g. x=[1 2 3; 4 5 6; 7 8 9] means
x= 1 2 3
4 5 6
7 8 9
So if u take x=[1 2 3; 4 5 6] and y=[7 8 9]'
then z=[x; y'] means
z= 1 2 3
4 5 6
7 8 9