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.
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)
So, the problem is simple to understand. Given any symbolic matrix of any dimmension I want to swap the elements in order to obtain one matrix of the same size, same elements but distributed differently.
For instance
syms a b c
A=[a b c;0 0 c]
swapping A we can get:
A=[b a c;0 c 0];
Using randperm(numel(A)) you can generate a random permutation of the numbers 1:numel(A), which are the indices in your matrix.
A(:)=A(randperm(numel(A)));
The A(:) on the left side is required to preserve the shape, otherwise you end up with a vector of the elements. If you wish to keep your matrix A unchanged:
B=A; %just to get a matrix of same size and datatype
B(:)=A(randperm(numel(A)));
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 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.
I have a single row matrix theta in Matlab with a couple hundred values; I would like to create a vector for each value in theta. Preferably I can simply store these values in a list so I can take dot products of individual elements in the list lator; how would I go about doing this?
For now, the code I have is
arrayfun(#(x) [sin(x),0,cos(x)],thetas,'UniformOutput',false);
which generates a bunch of [1x3 double]'s.
Instead of creating a cell array, you can also just create a numeric array of size numberOfThetas-by-3, like so:
A = [sin(thetas);zeros(size(thetas));cos(thetas)]'; %'# make n-by-3 by transposing
To calculate the dot product between any two vectors i and j, you can write
dotProduct = sum(A(i,:).*A(j,:));
You don't have to unnecessarily construct for loops to re-build the matrix as suggested by strictlyrude27. There is a nice inbuilt function cell2mat that does this for you in a single line.
Say your A is a 100 element cell, each of which holds a 1x3 vector. If you wanted to collect them such that each cell of A is a separate row, the command is simply
matrixA = cell2mat(A(:));
The output to your code is a cell array, whose elements are the 1x3 vectors you wanted. So suppose you assigned A = arrayfun(#(x) [sin(x),0,cos(x)],thetas,'UniformOutput',false); as you have up there. The vector corresponding with the ith element of array thetas may be accessed with A{i}. At this point you could use a for loop to construct a matrix whose ith column is the vector corresponding to the ith element of thetas.