I am trying to plot this function in matlab but I get the error: Error using *
Inner matrix dimensions must agree
Why is this happening?
My code:
H_s=2;
f_zero=2;
f=0:0.001:0.01;
S_f=(5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5)*exp(-(5/4)*f)
plot(f,S_f)
The first terms ((5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5)) evaluate to a 1x11 matrix, as does the last term (exp(-(5/4)*f)). The matrix multiplication operator * indeed requires the inner dimensions to match.
But you were probably trying to do element-wise multiplication .*:
S_f=(5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5).*exp(-(5/4)*f)
Related
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 am using Matlab R2011a. I have a matrix A which is of size 27*3355432. This matrix has a lot of zeros. I need to compute the comand eig(A'*A) but I can't even do the A'*A. I have tried using the sparse matrix B = sparse(A)and then computing B'*B but I get the error:
??? Error using ==> mtimes
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead
Truth is I am not a Matlab expert. Is there a way to produce such a database?
Doing some matlab work and keep getting: "Error using * Inner matrix dimensions must agree."
This is my code, what can I do to fix it?
a=2
b=5
x=[0:(pi/40):(pi/2)]
y=b*(exp(1).^(-a*x))*sin(b*x)*(0.012*(x.^4) -0.15*(x.^3) + 0.075*(x.^2) + 2.5*x)
You are mixing elemnt-wise (.*) and matrix (*) multiplication. Since you want element-wise multiplication for the vector x, you have to make sure that every vector/vector operation is element-wise. You just missed a few dots, it should be like this:
y=b*(exp(1).^(-a*x)).*sin(b*x).*(0.012*(x.^4)-0.15*(x.^3)+0.075*(x.^2)+2.5*x)
When I am using the following line of code in Matlab for the multiplication of two matrices
multiplied = singleMat * singleMatT;
Then it gives me this error..
??? Error using ==> mtimes
Integer data types are not fully supported for this operation.
At least one operand must be a scalar.
Please help me out for the multiplication of two matrices in matlab..
I guess Matlab doesn't support matrix multiplication of integer matrixes. Try:
multiplied = double(singleMat) * double(singleMatT);
or
multiplied = single(singleMat) * single(singleMatT);
if single precision is enough.
I am getting this error while running my code:
Error using ==> mldivide Matrix dimensions must agree.
Here is my code :
%make the plots of phase and group velocity vs discreteness of the grid
c=1;
a=input('Please enter the ratio cdt/dx : ')
figure(1)
R=2:40;
plot(R,phase_vel(R,a)/c)
xlabel('R=l/dx')
ylabel('u_phase/c')
%figure(2)
%plot(group_vel(R,a),R,0,40)
%xlabel('R=l/dx')
%ylabel('u_group/c')
and here are my functions :
function phase_velocity = phase_vel(R,a)
%numerical phase velocity of the discrete wave
c=1;
phase_velocity=(2*pi*c)/(R*knum(R,a));
end
function group_velocity =group_vel(R,a )
%numerical group velocity of the discrete wave
c=1;
group_velocity=(a*sin(knum(R,a)))/(sin(2*pi*a/R))
end
function knumber = knum(R,a)
%This is the k wave number
knumber=acos((1/a)^2*(cos(2*pi*a/R)-1)+1);
end
How can I resolve this error?
EDIT: I used . operator in every equation and i changed the limits of R=4:40
If your goal is to apply your formulas to each individual value in the vector R then you should be performing all of your computations using the element-wise arithmetic operators .*, ./, and .^ instead of the matrix operators *, /, and ^.
Your error is probably occurring in the first call to your function knum, specifically when you try to compute 2*pi*a/R. Since 2*pi*a is a single scalar value, you get an error when trying to perform matrix right division / using the row vector R. The really weird thing is the error message:
??? Error using ==> mldivide
Matrix dimensions must agree.
which implies you are using the matrix left division operator \, which you clearly aren't. I tested this in MATLAB R2010b and I get the same incorrect function name appearing in my message. I think this may just be a typo in the error message, and I've dropped a note to the MATLAB folks to take a look at it and clear it up.
I don't have the Symbolic Math Toolbox, but your problem seems to be that you are using plot, a function which can deal with arrays of numbers, and feeding it the result of a symbolic calculation. Have a look at the Matlab Help, where the Topic Creating Plots of Symbolic Functions suggests using ezplot(). Alternatively you need to evaluate your symbolic expression for certain input values to create an array of numbers that plot can deal with - but you can't use double() for that since it wouldn't know what numbers to plug into your variables.