Matlab - matrix dimensions error - matlab

I tried to write the following in Matlab
x=[0:0.1:1];
f=(x.^2)*sin(pi*x.^2);
But I was getting an error:
Error using * Inner matrix dimensions must agree.

Try
f=(x.^2) * sin(pi*x.^2)'
This yields a scalar.
f=(x.^2)' * sin(pi*x.^2)
becomes a matrix and
f=(x.^2)' .* sin(pi*x.^2)
a vector.

Related

Matlab ??? Error using ==> mldivide Matrix dimensions must agree

I have codes like these in Matlab
...
Kx=cell(10,10,10);
Kx2=cell(ee_max)
bX=cell(10,10)
bX2=cell(ee_max)
...
Kx{ee,ii,jj}=(1/36*VX{ee})*(alphX{ee,x}*bX{ee,ii}*bX{ee,jj}+alphX{ee,y}*cX{ee,ii}*cX{ee,jj}+alphX{ee,z}*dX{ee,ii}*dX{ee,jj})+VX{ee}*1/20*betaX{1+delXX{ii,jj}};
bX{ee,ii}=VX{ee}*(1/4)*f{ee}
...
Kx2{ee}=Kx{ee,:};
bX2{ee}=bX{ee,:};
phi_next=cell(4,4);
...
phi_next{ee}=bX2{ee}/Kx2{ee}
....
I got error at this line
phi_next{ee}=bX2{ee}/Kx2{ee}
as
??? Error using ==> mldivide
Matrix dimensions must agree.
so,what should I do?
regards
Use ./ instead of /.
The code is not complete so you may have other errors with other matrices dimensions.
If you find other errors you may use .* instead of * also when you multiply matrices with different dimensions.
Example:
A: 3 rows, 4 columns
B: 2 rows, 4 columns
C: 4 rows, 1 column
You can use * to multiply A and C (3x4)(4x1) or B and C (2x4)(4x1).
However you need .* to multiply A and B (3x4)(2x1).

A^t * A won't work in matlab?

I'm trying get A^T * A.
but if I try it, I get the error
Error using * MTIMES is not fully supported for integer classes.
At least one input must be scalar. To compute elementwise TIMES, use TIMES (.*) instead.
so I did A^T .* A and it gave me the error
Matrix dimensions must agree.
I don't get it? shouldn't ATA work fine?
A is
A = imread('C:\Users\user1\Documents\MATLAB\5\assignment05_A.jpg');
A = rgb2gray(A);
before trying the multiplication
How do i get a A^T * A matrix??
You can convert to an array of double precision with double then plot with imshow;
A = double(rgb2gray(A));
ATA = A'*A;
imshow(ATA,[]);

Matlab: Error when dividing the covariance matrix

I have an expression which is part of the log-likelihood expression for a Gaussian state space model
express =
\sum_{t=2}^T (x(t) - (A*x(t-1)))^2/2*Q
where T = 5, the number of samples/observations; x is a 2 by T matrix; Q is the covariance matrix of the process noise initialized using eye
x =
0.7311 -1.7152 0.2476 3.6643 -1.2870
0.4360 0.3554 0.1981 0.4168 0.2643
A =
0.1950 -0.9500
1.0000 0
Q =
1 0
0 1
I am getting this error:
Error using /
Matrix dimensions must agree.
This is how I have implemented:
numerator = sum((x(:,2:T)-(A*x(:,1:(T-1)))).^2)
numerator =
2.0732 3.0349 3.2291 1.5365
express = numerator / diag(2*diag(Q))
Should I be taking the diagonal or determinant of Q? Please help in correcting this part. Thank you.
You are squaring the term too early. The (') symbol means you need to take the complex conjugate of the term before multiplying it by the inverse of Q and then the term again. I believe you are trying to calculate this
in which case the term you want to sum over is the following,
term = x(:,2:T)-(A*x(:,1:(T-1));
result = term' * inv(Q) * term
the result of which is a 4x4 matrix. You can then sum this (over both directions I presume). From the (7) in the link you mention, you should need to follow this same procedure three times (for R, Q, and V).

Matlab error for matrix dimensions

I get an error
Error using -
Matrix dimensions must agree.
Error in Untitled6 (line 32)
temp=double(S_bar) - (repmat(mean_face, 1, num_images));
code :
for i=1:num_images
[m,n] = size(S(:,i))
[a,b] = size(repmat(mean_face, 1, num_images))
temp=double(S(:,i)) - (repmat(mean_face, 1, num_images));
size of S(:,i) is [45045 1] and size of repmat(mean_face, 1, num_images is [45045 45]
So i tried to use the transpose of S(:,i) so the dimensions become [1 45045] so that i can get a resulting matrix of [1 45]. But even when i use the transpose i get the same error. Why am i getting the error even though the matrix dimensions are correct?
You need to do what i believe is called "broadcasting", i.e. making sure elementwise operations on matrices are done on matrices of compatible sizes. One elegant way of doing this is using bsxfun :
temp=bsxfun(#minus,double(S(:,i)),(repmat(mean_face, 1, num_images)));
You could also use repmat on your double(S(:,i)) so that it has the right number of columns.
Hope this helps,
Tepp

Why the valid looking statement gives error in MATLAB?

It's from this question。
Why the two solutions doesn't work, though it looks very valid for me:
>> t = -pi:0.1:pi;
>> r = ((sin(t)*sqrt(cos(t)))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2 ;
??? Error using ==> mtimes
Inner matrix dimensions must agree.
>> t = -pi:0.1:pi;
>> r = ((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2 ;
>> plot(r,t)
??? Error using ==> plot
Vectors must be the same lengths.
What's wrong with the above?
The * operator is the matrix multiplication operator, which requires its operands to have matching inner matrix dimensions. The .* operator is the element-wise multiplication operator, which requires its operands to have the same size (or for one to be a scalar) so it can perform multiplication on each matching pair of elements. See this link for more detail.
Also, I don't get the plotting error you do when I run the second solution. I just get this warning:
Warning: Imaginary parts of complex X and/or Y arguments ignored