When multiplied A = eye(3,3) with x = sym('x',[3,3]) as in this form (A.*x), I got a result. But when multiply A = eye(3,3) with y = sym('y',[3,2]) I got an error while from the matrix multiplication point of view it is correct. Why it is so?
.* is not matrix multiplication, it is element-wise multiplication. You want to do A*x.
Related
[y,fs]=wavread('C:\Users\Mohamed\Desktop\sinesweeprec.wav')
[x,fs]=wavread('C:\Users\Mohamed\Desktop\sinesweep.wav')
a=fft(x)
b=fft(y)
h=ifft(b/a)
So I use this code in order to get the impulse response of a room but I get this error ('Error using / Matrix dimensions must agree')
can someone please help and how to solve it.
You might want to do ./ to do per element division.
h = ifft(b./a)
Remember in 2D, if you do matrix multiplication:
3x4 * 4x3 = 3x3 matrix. And a 3x4 * 3x4 isn't possible, but you could to a per pixel multiplication to have 3x4 .* 3x4 = 3x4
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));
I need to evaluate the integral from x=-1 to x=1 for the function x*e^(x)*sin(pi*x). To do so, I'm using the Gaussian Quadrature method. This method can be summarized as the summation of the (weights)*(function evaluated at given roots) from k=1 to k=n where n=30.
In my code below, the roots are found in the column vector LegendreGaussQuadratureConstants(n).x. I'm taking the transpose because I want to perform element by element multiplication on the weights, which are stored in the row vector LegendreGaussQuadratureConstants(n).w
My code:
fx = #(x) x .* e.^(-x) .* sin(pi .* x);
for k = 1:50
Leg(k) = (fx(LegendreGaussQuadratureConstants(k).x))' .* LegendreGaussQuadratureConstants(k).w;
endfor
The problem is that it's giving me the error in the title, implying that there is some problem with using a scalar value of k and that it should match up with the size of values being multiplied, but that makes no sense...
You said it yourself in your question
the roots are found in the column vector LegendreGaussQuadratureConstants(n).x. I'm taking the transpose because I want to perform element by element multiplication on the weights, which are stored in the row vector LegendreGaussQuadratureConstants(n).w.
You're taking the element-wise product of two vectors and the result is also going to be a vector. However, you then try to assign it to a scalar, Leg(k) which produces your error.
You either need to store these vectors in a 2D version of Leg
for k = 1:50
Leg(k,:) = (fx(LegendreGaussQuadratureConstants(k).x))' .* LegendreGaussQuadratureConstants(k).w;
endfor
or perform some other operation on the element-wise multiplication result to get it to be a scalar.
for k = 1:50
Leg(k) = sum((fx(LegendreGaussQuadratureConstants(k).x))' .* LegendreGaussQuadratureConstants(k).w);
endfor
I am trying to solve a second order differential using ODE45 in Matlab with matrix as inputs. I am struck with couple of errors that includes :
"In an assignment A(I) = B, the number of elements in B and
I must be the same."
Double order differential equations given below:
dy(1)= diag(ones(1,100) - 0.5*y(2))*Co;
dy(2)= -1 * Laplacian(y(1)) * y(2);
Main function call is:
[T,Y] = ode45(#rigid,[0.000 100.000],[Co Xo]);
Here, Co is Matrix of size 100x100 and Xo is a column matrix of size 100x1. Laplacian is a pre-defined function to compute matrix laplacian.
I will appreciate any help in this. Should I reshape input matrices and vectors to fall in same dimensions or something?
Your guess is correct. The MATLAB ode suite can solve only vector valued ode, i.e. an ode of the form y'=f(t,y). In your case you should convert y, and dy, back and forth between a matrix and an array by using reshape.
To be more precise, the initial condition will be transformed into the array
y0 = reshape([Co Xo], 100*101, 1);
while y will be obtained with
y_matrix = reshape(y, 100, 101);
y1 = y_matrix(:,1:100);
y2 = y_matrix(:,101);
After having computed the matrices dy1 and dy2 you will have to covert them in an array with
dy = reshape([dy1 dy2], 100*101, 1);
Aside from the limitations of ode45 your code gives that error because, in MATLAB, matrices are not indexed in that way. In fact, if you define A = magic(5), A(11) gives the eleventh element of A i.e. 1.
I have a M x N matrix. I want to multiply each of the N columns by a M x M matrix. The following does this in a loop, but I have no idea how to vectorize it.
u=repmat(sin(2*pi*f*t),[n 1]);
W = rand(n);
answer = size(u);
for i=1:size(u,2)
answer(:,i) = W*u(:,i);
end
You simply need to multiply the two matrices:
answer = W*u;
Think about it: in every iteration of your loop you multiply a matrix by a vector. The result of that operation is a vector, which you save into your answer in column i. Matrix multiplication is a similar thing: you can understand it as multiplication of a matrix (W) by a set of vectors, which form your matrix u.
So your code is good, just remove the loop :)