"Inner matrix dimensions must agree" MATLAB error - matlab

I am using the following equation in Matlab:
k=10e-10:0.01:1.5;
Ck2=(0.5*((i*k+0.135)*(i*k+0.651)))./((i*k+0.0965)*(i*k+0.4555));
plot(k,imag(Ck2));
plot(k,real(Ck2));
I did not define "i" so MATLAB assumes is an imaginary number in my equation as expected. I am trying to plot the real & imaginary parts of the equation against the range of k.
I am getting an error saying: Inner matrix dimensions must agree. I already tried to use the "." operator before the multiplication operator to multiply each element but I didn't succeed. Any help would be appreciated it.
Thank you in advanced.

Since k is a vector, when you multiply k * k, you are multiply 2 vectors using matrix multiplication. With matrix multiplication, you multiply an j x k size matrix by a k x l size matrix, and get a j x l result.
But here you are multiplying 1 x 150 by 1 x 150, so the dimensions don't line up for proper matrix multiplication. Instead, using .* will perform pairwise multiplication between each of the elements.
Try this:
k = 10e-10:0.01:1.5; % 1 x 150 length vector
Ck2= (0.5*((i*k+0.135) .* (i*k+0.651))) ./ ((i*k+0.0965) .* (i*k+0.4555));

Related

What does the operation A'\B' do if A and B are both row vectors of the same size?

[1 2 1]'\[1 2 3]' This is a numerical example. This example gives an answer of 1.333
From the documentation:
x = A\B
If A is a rectangular m-by-n matrix with m ~= n, and B is a matrix with m rows, then A\B returns a least-squares solution to the system of equations A*x= B.
Furthermore the ' compute the conjugate transposed of a matrix. In your case you have two real matrices so you just get the transposed each time.

Dividing matrix values by the size of the matrix produces error

I have a matrix 1080x1920 double. I want to divide the matrix by it's size. However, it returns an error.
[n m] = size(a);
a/[n m];
**Error using /
Matrix dimensions must agree.**
Any help is appreciated.
When you use [n m]=size(a), n is the number of line and m is the number of column (or row) : then [n m] is a line-matrix with 2 elements.
If I'm right, you're trying to divide a by the number of element in a. So wheter you do a/(n*m) or you can use too the function length since n*m=length(a) (the length function gives you the number of element in the input matrix.
Otherwise, if you're trying to do the matrix multiplication a/[n m] or a*([n m]^-1), the dimension of the matrices a and [n m] have to be mathematically consistent to perform such a matrix multiplication.

on symmetric positive semi-definiteness of covariance matrices in matlab

Hi everybody I have this problem:
I have Dataset of n vectors each has D dimensions.
I also have a covariance matrix of size D*D, Let It be C.
I perform the following action:
I choose K vectors from the dataset, and also choose E dimensions randomly. Let M be the sample covariance of the selected data on the selected dimensions.so M is a E*E matrix.
let P be the partial covariance matrix corresponding to the dimensions E of C, ie. C(E,E) in matlab
is the following matrix positive semi definite?:
X = (1-a)P + aM
where a is a constant like 0.2.
I sometimes get the following error when using mvnrnd(mean,X) :
SIGMA must be a symmetric positive semi-definite matrix
My code is:
%%%Dims are randomly choosen dimensions
%%%Inds are randomly choosen Indexes form {1, 2, ...,n}
%%% PP are n D dimensional vectors, composing my data set PP is n*D
%%% Sigmaa is a D*D covariance matrix
co = cov(PP(Inds,Dims));
me = mean(PP(Inds,Dims));
Bettaa = 0.2;
sigmaaDims = sigmaa(Dims,Dims);
sigmaaDims = (1-Bettaa)*sigmaaDims + (co)*Bettaa;
Tem = mvnrnd(me,sigmaaDims);
Simply looking at the matrix dimensions It is not possible to tell if a matrix is positive semi-definite.
To find out if a given matrix is positive semi-definite, you must check if It's eigenvalues are non-negative and it's symmetry:
symmetry = issymmetric(X);
[~,D]=eig(X);
eigenvalues = diag(D);
if all(eigenvalues>0) & symmetry
disp('Positive semi-definite matrix.')
else
disp('Non positive semi-definite matrix.')
end
Where X is the matrix you are interested in.
Note that if you use the weaker definition of a positive definite matrix (see Extention for non symmetric matrices section), X does not need to be symmetric and you would end up with:
[~,D]=eig(X);
eigenvalues = diag(D);
if all(eigenvalues>=0)
disp('Positive semi-definite matrix.')
else
disp('Non positive semi-definite matrix.')
end

Find a matrix that gives same result when multiplied by a constant or another matrix

I have got a problem like A*x=lambda*x, where A is of order d*d, x is of order d*c and lambda is a constant. A and lambda are known and the matrix x is unknown.
Is there any way to solve this problem in matlab?? (Like eigen values but x is a d*c matrix instead of being a vector).
If I've understood you correctly, there will not necessarily be any solutions for x. If A*x=lambda*x, then any column y of x satisfies A*y=lambda*y, so the columns of x are simply eigenvectors of A corresponding to the eigenvalue lambda, and there will only be any solutions if lambda is in fact an eigenvalue.
From the documentation:
[V,D] = eig(A) produces matrices of eigenvalues (D) and eigenvectors
(V) of matrix A, so that A*V = V*D. Matrix D is the canonical form of
A — a diagonal matrix with A's eigenvalues on the main diagonal.
Matrix V is the modal matrix — its columns are the eigenvectors of A.
You can use this to check if lambda is an eigenvalue, and find any corresponding eigenvectors.
You can transform this problem. Write x as vector by by using x(:) (has size d*c x 1). Then A can be rewritten to a d*c x d*c matrix which has c versions of A along the diagonal.
Now it's a simple eigenvalue problem.
Its actually trivial. Your requirement is that A*X = lambda*X, where X is an array. Effectively, look at what happens for a single column of X. If An array X exists, then it is true that
A*X(:,i) = lambda*X(:,i)
And this must be true for the SAME value of lambda for all columns of X. Essentially, this means that X(:,i) is an eigenvector of A, with corresponding eigenvalue lambda. More importantly, it means that EVERY column of X has the same eigenvalue as every other column.
So a trivial solution to this problem is to simply have a matrix X with identical columns, as long as that column is an eigenvector of A. If an eigenvalue has multiplicity greater than one (therefore there are multiple eigenvectors with the same eigenvalue) then the columns of X may be any linear combination of those eigenvectors.
Try it in practice. I'll pick some simple matrix A.
>> A = [2 3;3 2];
>> [V,D] = eig(A)
V =
-0.70711 0.70711
0.70711 0.70711
D =
-1 0
0 5
The second column of V is an eigenvector, with eigenvalue of 5. We can arbitrarily scale an eigenvector by any constant. So now pick the vector vec, and create a matrix with replicated columns.
>> vec = [1;1];
>> A*[vec,vec,vec]
ans =
5 5 5
5 5 5
This should surprise nobody.

product of all argument and absolut values of matrix in Matlab

I have a complex matrix A (NxN). In Matlab eig(A) will give me all the complex eigenvalues of the matrix. Now I am interesting in finding the absolute value (r) and the argument (\phi) of each complex eigenvalues (each eigenvalue has its own r=abs(Z) and \phi=arg(Z)) .
How can I write the following product expression:
\prod_j (sin(\phi_j)+r^(1/2)_j where the index j run over all the eigenvalues of the matrix A.
To get r and phi, simply use the Matlab functions abs and angle, like so ...
z = eig(rand(5));
r = abs(z)
phi = angle(z)
Then you can do whatever you need to do with the resulting vectors.
For example, the product of the quantity sin(phi) + sqrt(r) for all phi and r pairs would be:
prod( sin(phi) + sqrt(r) )
(Note, vectorization of the sin and sqrt function removes the need for any looping.)