I'm trying to multiply two complex matrices (b+c*i), but I'm getting no results.
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==> impedancaZ at 14
l=mtimes(R1,h)
I don't understand this error, as matrix dimension are the same (2 coluoms and 9 rows)
Can you help me?
Inner matrix dimensions must agree means that the inner dimensions of the matrices must match. If the first matrix has dimensions 2x9, then the second would need to be 9x(something). that's just basic liner algebra/matrix multiplication. In that case, you'll need to figure out what the second array should be. Maybe it's the transpose of what you expect it to be; instead of x*y, you might want x*y' (see the "prime" marker after the y?
Alternatively, maybe you want a "scalar multiply" rather than a "matrix multiply" for this. That is, you don't want to multiply the matrices x and y in the "linear algebra" sense, but you just want to multiply the elements of the arrays, element by element. In that case, you'd do x.*y (see the dot before the *?).
Unfortunately, I can't tell which is really correct for your situation without more context. You'll either have to supply some more information or figure it out yourself from the hints I've given.
Related
so I'm trying to study a code written in MATLAB. And there are these two strange lines of code, which I can't seem to understand, maybe someone could help me out? I'm new to MATLAB, I'm coding in C# most of the time.
As far as I know diag(A) means that it takes the members of main diagonal of matrix A. But what about the other parts of the line? Especially the 1./ operation, what does it do?
In the code below
A is a 4x4 matrix, which stores double type values, b is the coefficients vector and alpha is a freely chosen vector (10, 5, 4, 2).
Atld=diag(1./diag(A))*A-diag(alpha)
btld=diag(1./diag(A))*b
diag(A) returns a vector with the diagonal elements of matrix A
./ is the element-wise division operator, so 1./diag(A) inverts the elements from this vector.
diag(1./diag(A)) returns the diagonal matrix from that vector
So, basically, diag(1./diag(A)) is a matrix with the inverse of the diagonal of A on its diagonal, and zeros everywhere else.
I am attempting to find The cosines of the angles θj between a query vector q and the document vectors aj in which j refers to the column number.
Here is the formula, I am attempting to write in matlab.
And here is my attempt at the code thus far:
cosval = (R(:,i)'*(v(Q(:,i))'))./(norm(v)*norm(R(:,i)));
And the issue I am running into:
Subscript indices must either be real positive integers or logicals.
And here are the two matrices I am attempting this with: The matrices are separated by the line, sorry for my poor paint skills. Could the issue I am having be because my matrices do not consist of real integers or is this just the way matlab displays matrices and the issue is with my calculations line?
Where do you define i ? Are the pictured matrices R and Q? We need more information to answer fully.
The most likely problem I can see is it appears you're using values from Q as subscript indices for v, which makes no sense if those values are not integers: you can't have the 5.774th entry in a vector. Going by the formula posted and assuming Q is a matrix it looks more likely that you want to write R(:,i)'*(Q'*v), although I don't know what the capital A subscript means in your formula.
Alternatively, if you haven't assigned a variable i, it could be that matlab is interpreting it as the imaginary unit. Either error would lead to the complaint about indices needing to be 'real positive integers'.
In my current analysis, I am trying to multiply a matrix (flm), of dimension nxm, with the inverse of a matrix nxmxp, and then use this result to multiply it by the inverse of the matrix (flm).
I was trying using the following code:
flm = repmat(Data.fm.flm(chan,:),[1 1 morder]); %chan -> is a vector 1by3
A = (flm(:,:,:)/A_inv(:,:,:))/flm(:,:,:);
However. due to the problem of dimensions, I am getting the following error message:
Error using ==> mrdivide
Inputs must be 2-D, or at least one
input must be scalar.
To compute elementwise RDIVIDE, use
RDIVIDE (./) instead.
I have no idea on how to proceed without using a for loop, so anyone as any suggestion?
I think you are looking for a way to conveniently multiply matrices when one is of higher dimensionality than the other. In that case you can use bxsfun to automatically 'expand' the smaller matrix.
x = rand(3,4);
y = rand(3,4,5);
bsxfun(#times,x,y)
It is quite simple, and very efficient.
Make sure to check out doc bsxfun for more examples.
I use the function below to generate the betas for a given set of guess lambdas from my optimiser.
When running I often get the following warning message:
Warning: Matrix is singular to working precision.
In NSS_betas at 9
In DElambda at 19
In Individual_Lambdas at 36
I'd like to be able to exclude any betas that form a singular matrix form the solution set, however I don't know how to test for it?
I've been trying to use rcond() but I don't know where to make the cut off between singular and non singular?
Surely if Matlab is generating the warning message it already knows if the matrix is singular or not so if I could just find where that variable was stored I could use that?
function betas=NSS_betas(lambda,data)
mats=data.mats2';
lambda=lambda;
yM=data.y2';
nObs=size(yM,1);
G= [ones(nObs,1) (1-exp(-mats./lambda(1)))./(mats./lambda(1)) ((1-exp(-mats./lambda(1)))./(mats./lambda(1))-exp(-mats./lambda(1))) ((1-exp(-mats./lambda(2)))./(mats./lambda(2))-exp(-mats./lambda(2)))];
betas=G\yM;
r=rcond(G);
end
Thanks for the advice:
I tested all three examples below after setting the lambda values to be equal so guiving a singular matrix
if (~isinf(G))
r=rank(G);
r2=rcond(G);
r3=min(svd(G));
end
r=3, r2 =2.602085213965190e-16; r3= 1.075949299504113e-15;
So in this test rank() and rcond () worked assuming I take the benchmark values as given below.
However what happens when I have two values that are close but not exactly equal?
How can I decide what is too close?
rcond is the right way to go here. If it nears the machine precision of zero, your matrix is singular. I usually go with:
if( rcond(A) < 1e-12 )
% This matrix doesn't look good
end
You can experiment with a value that suites your needs, but taking the inverse of a matrix that is even close to singular with MATLAB can produce garbage results.
You could compare the result of rank(G) with the number of columns of G. If the rank is less than the column dimension, you will have a singular matrix.
you can also check this by:
min(svd(A))>eps
and verifying that the smallest singular value is larger than eps, or any other numerical tolerance that is relevant to your needs. (the code will return 1 or 0)
Here's more info about it...
Condition number (Maximal singular value/Minimal singular value) is another good method:
cond(A)
It uses svd. It should be as close to 1 as possible. Very large values mean that the matrix is almost singular. Inf means that it is precisely singular.
Note that almost all of the methods mentioned in other answers use somehow svd :
There are special tools designed for this problem, appropriately called "rank revealing matrix factorizations". To my best (albeit a little old) knowledge, a good enough way to decide whether a n x n matrix A is nonsingular is to go with
det(A) <> 0 <=> rank(A) = n
and use a rank-revealing QR factorization of A:
AP = QR
where Q is orthogonal, P is a permutation matrix and R is an upper triangular matrix with the property that the magnitude of the diagonal elements is decreased along the diagonal.
I tried to update some part of a matrix, I got the following error message:
??? Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts
My code tries to update some values of a matrix that represent a binary image. My code is as follows:
outImage(3:5,2:4,1) = max(imBinary(3:5,2:4,1));
When I delete last parameter (1), this time I get the same error. I guess there is a mismatch between dimensions but I could not get it. outImage is a new object that is created at that time (I tried to create it before, but nothing changed). What may be wrong?
You mention in one of your comments on another answer that you are trying to create your own dilation algorithm, and therefore want to take the maximum value in a 3-by-3-by-1 submatrix and replace the values in that submatrix with the maximum value. The function MAX will by default operate along the columns of your submatrix, which will give you a 1-by-3 matrix (i.e. the maximum values of the columns of your 3-by-3-by-1 matrix). The error results because MATLAB can't assign a 1-by-3 matrix to a 3-by-3-by-1 matrix.
One solution is to call MAX again on your 1-by-3 matrix to get a scalar value, which you can then assign to each element of your 3-by-3-by-1 submatrix without error:
outImage(3:5,2:4,1) = max(max(imBinary(3:5,2:4,1)));
On the rhs of your equation you take the max of a 3x3x1 sub-matrix, which returns a 1x3 vector. You then try to assign this to a 3x3x1 sub-matrix. A singleton subscript is one with the value 1. So the rhs has 1 non-singleton subscript, and the lhs has 2. Matlab can't figure out how to expand a 1x3 matrix to fill a 3x3x1 space.
I'm not entirely sure what you want to do, so I won't guess a solution. Do you want to make 3 copies of the rhs and put one into each row of the sub-matrix on the lhs ? Or are you trying to construct a 3x3x1 matrix on the rhs ?
Do you want to fill all indexed elements in outImage by maximum value for each column of rhs expression? You can expand the row you get on rhs with REPMAT:
outImage(3:5,2:4,1) = repmat(max(imBinary(3:5,2:4,1)),3,1)
outImage(3:5,2:4) works as well.
I got the same error before, and what I have done was defining the left hand matrix before. I don't know if you have the same case but you can try the following:
outImage=Zeros(M,N,K);
M, N, and K are the dimensions that you have. Then just type:
outImage(3:5,2:4,1) = max(max(imBinary(3:5,2:4,1)));