senshi nakamora
senshi nakamora

Reputation: 65

Error in inner matrix dimensions in matlab

I was trying to correct the below code but did not succeed .. Matlab says :

Error using  * 
Inner matrix dimensions must agree.

Error in 
Atten = a*l*f;

Here is the code

a=[0.6 1.89 4.1 0.9 0.8 3]; 
l=[0.5 0.5 0.5 0.5 0.5 0.5];  
f=[1000000:4000000:21000000]; 
Atten = a*l*f; 
plot(f,Atten)

I even tried with l=[0.4]; but it did not work How to correct the code?

Thanks

Upvotes: 0

Views: 33

Answers (3)

Kami Kaze
Kami Kaze

Reputation: 2080

If you want to multiply element-wise, you can use . in front of the *

Upvotes: 0

MMF
MMF

Reputation: 5931

There is a mismatch in your matrices dimensions. Make sure to multiply matrices with right dimensions.

You can use the .' operator to transpose a matrix.

Upvotes: 0

obchardon
obchardon

Reputation: 10792

Here it's more a mathematical problem than a programming error.

You're trying to multiply 3 [1x6] matrix, of course it's not possible.

Perhaps that you want this operation

[1x6]*[6x1]*[1x6] = [1x6]

instead of

[1x6]*[1x6]*[1x6] = Impossible

In that case you can use this operator .' to transpose your matrix:

Atten = a*l.'*f; 

result:
5.6450e+06   2.8225e+07   5.0805e+07   7.3385e+07   9.5965e+07   1.1855e+08

In that situation l.' will be a [6x1] matrix and the operation is now possible

More information about this operator: Tranpose vectore or matrix

Upvotes: 1

Related Questions