I was wondering if there is a way to subset a matrix in EIGEN into another Matrix based on some condition.
For example in MATLAB, one can do this: Let A be a matrix of size 5-by-5.
B=A(1:3,2:3)
This will create a new matrix B from A with dimensions 3-by-2 and with elements from A at the relevant indices.
Related
I have a matrix of data A with dimension m-by-n. Where m is the number of data vectors and n is the dimension if each data vector (so they are arranged by rows).
If I do
[U,S,V] = svds(A, k);
the U matrix will be m-by-k, but I was expecting a n-by-k matrix (to be used to project any 1-by-n original vector to a 1-by-k one).
What I am doing wrong? Should I arrange data by column? (i.e., use A' instead of A)
I want to define a matrix in MATLAB such that each element in matrix has 3 elements ,like if i have a matrix m=[a b;c d] a 2x2 matrix such that the element a has values (k,l) like wise b has value (j,m) and so on .
You can introduce 3-dimensonal matrix. If your matrix size is NxM and each element of this matrix contains k elements, you define matrix B of the size NxMxK. By calling B(2,3,:) you will access all the elements of the entry (2,3).
Alternatively, you can define a cell matrix, so that every entry is cell array.
If you want each element of your matrix to be consisted of only two real elements, you can define complex-valued matrix.
In Matlab, I have created a matrix A with size (244x2014723)
and a matrix B with size (244x1)
I was able to calculate the correlation matrix using corr(A,B) which yielded in a matrix of size 2014723x1. So, every column of matrix A correlates with matrix B and gives one row value in the matrix of size 2014723x1.
My question is when I ask for a covariance matrix using cov(A,B), I get an error saying A and B should be of same sizes. Why do I get this error? How is the method to find corr(A,B) any different from cov(A,B)?
The answer is pretty clear if you read the documentation:
cov:
If A and B are matrices of observations, cov(A,B) treats A and B as vectors and is equivalent to cov(A(:),B(:)). A and B must have equal size.
corr
corr(X,Y) returns a p1-by-p2 matrix containing the pairwise correlation coefficient between each pair of columns in the n-by-p1 and n-by-p2 matrices X and Y.
The difference between corr(X,Y) and the MATLABĀ® function corrcoef(X,Y) is that corrcoef(X,Y) returns a matrix of correlation coefficients for the two column vectors X and Y. If X and Y are not column vectors, corrcoef(X,Y) converts them to column vectors.
One way you could get the covariances of your vector with each column of you matrix is to use a loop. Another way (might be in-efficient depending on the size) is
C = cov([B,A])
and then look at the first row (or column) or C.
See link
In the more about section, the equation describing how cov is computed for cov(A,B) makes it clear why they need to be the same size. The summation is over only one variable which enumerates the elements of A,B.
I have a m-rows by n-columns matrix in matlab. One of the columns (#2) contain the values of VIP and I want to obtain the matrix that only contain the rows of the original matrix where VIP>1.
How can I find this new matrix?
I have two 50 x 6 matrices, say A and B. I want to assign weights to each element of columns in matrix - more weight to elements occurring earlier in a column and less weight to elements occurring later in the same column...likewise for all 6 columns. Something like this:
cumsum(weight(row)*(A(row,col)-B(row,col)); % cumsum is for cumulative sum of matrix
How can we do it efficiently without using loops?
If you have your weight vector w as a 50x1 vector, then you can rewrite your code as
cumsum(repmat(w,1,6).*(A-B))
BTW, I don't know why you have the cumsum operating on a scalar in a loop... it has no effect. I'm assuming that you meant that's what you wanted to do with the entire matrix. Calling cumsum on a matrix will operate along each column by default. If you need to operate along the rows, you should call it with the optional dimension argument as cumsum(x,2), where x is whatever matrix you have.