I have two vectors, X of bases and N of exponents. I want to get the matrix of all values e = xn for each x in X and n in N.
For example, the following input:
X = [2 3 4]'
N = [1 2 3]
should produce:
ans = [2 4 8; 3 9 27; 4 16 64]
Is there a way to get this without looping (just like you can get all values of x×n by using the column by row product)?
Use bsxfun:
bsxfun(#power, X, N)
This assumes that X is a column vector and N is a row vector. If you want to guarantee that, use the following syntax which is more robust:
bsxfun(#power, X(:), N(:).')
This is probably a bit sloppier than the bsxfun answer, but you could use meshgrid:
E = X.^(meshgrid(N)')
This assumes both X and N are row vectors. If both are column vectors then it becomes:
E = X.^(meshgrid(N))
Related
I need to understand a part of the GMRES algorithm written in these languages. What makes me a problem is the following piece:
y = H(1:k,1:k) \ beta(1:k);
x = x + Q(:,1:k)*y;
Can someone explain what does it mean in extended form.
Thank you in advance.
For what concerns the first equation:
H(1:k,1:k) = sub-matrix of matrix H that you obtain by taking rows from 1 (beginning) to k and columns from 1 (beginning) to k
beta(1:k) = sub-vector of vector beta that you obtain by taking elements from 1 (beginning) to k
y = is a matrix obtained by solving a symbolic matrix left division between sub-matrix of H and the sub-vector of beta
For what concerns the second equation:
Q(:,1:k) = sub-matrix of matrix Q with all the rows and columns from 1 (beginning) to k
x = a matrix that is obtained by adding to it's previous value the result of the multiplication between the sub-matrix of matrix Q and y
Indexing in Matlab is 1-based, not 0-based. So index 1 corresponds to the first element of whatever you are working with. Example of sub-matrix by indexing:
A = [
2 3 4;
1 2 3;
3 4 4
];
B = A(1:2,1:2);
B is then equal to:
[
2 3;
1 2
];
C = A(:,1:2);
C is then equal to:
[
2 3;
1 2;
3 4
];
That weird division symbol represents a matrix left division (for more information: mathworks.com/help/symbolic/mldivide.html): X = A\B solves the symbolic system of linear equations in matrix form: A*X = B for X.
Lets say I have matrices A = [1 2; 3 4], B = [4 3; 2 1]. I want to multiply each column from matrix A ([1; 3], [2; 4]) by the corresponding row in matrix B ([4 3], [2 1]) and sum resulting matrices. I have came up with the following code:
C = zeros(size(A));
for i = 1 : size(A, 1)
C = C + A(:, i) * B(i, :);
end
Could it be rewritten using some math trick or matlab function to get rid of the for loop?
I see there is unclarity in my question regarding the result I want. The result should exactly mimic provided Matlab code, therefore I seek one matrix which is given by the matrix summation of the intermediate matrices that are created by multiplying each column vector with corresponding row vector from both matrices. For this specific example, it would be given by
C = A(:, 1) * B(1, :) + A(:, 2) * B(2, :);
I am just looking for some generic, for-loop less version for any matrices of compatible dimensions.
I just tried out my suggestion in the comments, and it seems to work with this octave tester:
Short form (only works in Octave):
A = [1 2; 3 4], B = [4 3; 2 1]
X = sum((A * B)(:))
Long form (Matlab):
A = [1 2; 3 4]
B = [4 3; 2 1]
C = A * B % Stop here if you want the exact result from your Matlab code
x = sum(C(:)) % To get the sum of the resulting matrix
Sources:
https://www.tutorialspoint.com/matlab/matlab_matrix_multiplication.htm
https://www.mathworks.com/matlabcentral/newsreader/view_thread/51252
Update, based on your update:
Output of A * B:
8 5
20 13
Output of your code:
8 5
20 13
It appears that
C = zeros(size(A));
for i = 1 : size(A, 1)
C = C + A(:, i) * B(i, :);
end
is equivalent to the matrix multiplication
C = A*B
for square matrices A and B.
You can also do this in MATLAB, to get the sum.
C=ones(1,2)*A*B*ones(2,1)
The general form would be
C=ones(1,size(A,1))*(A*B)*ones(size(B,2),1);
EDIT
I see you updated your question for clarity. The matrix product can be calculated directly
C = A*B;
as pointed out by jodag.
This works provided you follow rules of linear algebra where inner dimensions of your matrices are the same (i.e. when number of columns in A match the number of rows in B; size(A,2)==size(B,1)).
I have got a matrix M with R rows and C*k columns. I want to create another matrix F with R rows and C columns, so that
F(:,j) = min(M(:,(j-1)*k+1:j*k),[],2)
In other words first column of F should be min of first k columns of M.
Second column of F should be min of second k columns of M and so on.
I am trying to reshape matrix M to R*C x k matrix D find min and reshape it again to R x C matrix.But I cannot properly reshape M to F, so that F(i, :) = M(b, (j-1)*k+1:j*k)
Here is a small example:
k=2;
M = [1 2 3 4; 5 6 7 8; 9 10 11 12;];
then
F = [1 3; 5 7; 9 11];
Your best bet is probably to reshape it to an R x k x (C / k) matrix and then compute the min along the second dimension and then squeeze the result to remove the now-empty second dimension.
F = squeeze(min(reshape(M, size(M, 1), k, size(M, 2)/k), [], 2))
I have two vectors, X of bases and N of exponents. I want to get the matrix of all values e = xn for each x in X and n in N.
For example, the following input:
X = [2 3 4]'
N = [1 2 3]
should produce:
ans = [2 4 8; 3 9 27; 4 16 64]
Is there a way to get this without looping (just like you can get all values of x×n by using the column by row product)?
Use bsxfun:
bsxfun(#power, X, N)
This assumes that X is a column vector and N is a row vector. If you want to guarantee that, use the following syntax which is more robust:
bsxfun(#power, X(:), N(:).')
This is probably a bit sloppier than the bsxfun answer, but you could use meshgrid:
E = X.^(meshgrid(N)')
This assumes both X and N are row vectors. If both are column vectors then it becomes:
E = X.^(meshgrid(N))
a = [1 2; 3 4; 5 6] I want to extract the first and third row of a, so I have x = [1; 3] (indices of rows).
a(x) doesn't work.
Like this: a([1,3],:)
The comma separates the dimensions, : means "entire range", and square brackets make a list.
In MATLAB if one parameter is given when indexing, it is so-called linear indexing. For example if you have 4x3 matrix, the linear indices of the elements look like this, they are growing by the columns:
1 5 9
2 6 10
3 7 11
4 8 12
Because you passed the [1 3] vector as a parameter, the 1st and 3rd elements were selected only.
When selecting whole columns or rows, the following format shall be used:
A(:, [list of columns]) % for whole columns
A([list of rows], :) % for whole rows
General form of 2d matrix indexing:
A([list of rows], [list of columns])
The result is the elements in the intersection of the indexed rows and columns. Results will be the elements marked by X:
A([2 4], [3 4 5 7])
. . C C C . C
R R X X X R X
. . C C C . C
R R X X X R X
Reference and some similar examples: tutorial on MATLAB matrix indexing.
x = a([1 3]) behaves like this:
temp = a(:) % convert matrix 'a' into a column wise vector
x = temp([1 3]) % get the 1st and 3rd elements of 'a'
you could write a loop to iterate across the rows of the matrix:
for i = [1,3]
a(i,:)
end
type this: a([1 3],[1 2])
the output is
ans =
1 2
5 6