error in Matlab function: Inner matrix dimensions must agree

Hi, I wrote this function in Matlab:
function f = objfun(x,a,b)
x1 = x(1:24);
y1 = x(25:end);
i = 1:24;
if x(i)~=0
x(i+24)=0
end
if x(i+24)~=0
x(i)=0
end
f = -mean (x1*a')+mean(y1*b');
end
x1, y1, a and b are 1x24 vectors.
I get an error message that Inner matrix dimensions must agree! I don't know what is the problem, because when I write -mean (x1*a')+mean(y1*b') in the command window it gives me the correct answer, but when I run the code it gives error message about f. I also tried with .* but still I get the same error. Could you please help me to find the reason? Thanks!

3 Comments

Are you sure the dimensions are consistent?
y1 = x(25:end)
seems to imply that x is not 1*24.
Place a breakpoint before
f = -mean (x1*a')+mean(y1*b');
and see for yourself.
x must be 1x48 vector, I believe
Thanks for your answers. x is 1x48 vector, but x1 and y1 which are used in the f are 1x24 vectors. It means the first 24 elements of x is x1 and from 25 to 48 are y1. I think it is a bit confusing. Is there a better way that I can define f function?

Sign in to comment.

Answers (1)

This function does very little. It is equivalent to this:
function f = objfun(x,a,b)
x1 = x(1:24);
y1 = x(25:end);
f = -(x1*a')+(y1*b');
end
You should really watch out with that last line. you're calculating the product, so following it up with mean is meaningless. I have assumed in my edit of your program that you meant an element-wise product.
You might mean the following function:
%generate random input (remove for your actual function)
x=round(rand(1,48));
a=rand(1,24);
b=rand(1,24);
x1 = x(1:24);
y1 = x(25:end);
%filter x1 and y1 (1 of them must be 0 for each position)
%the order of these two lines matters!!
x1(y1~=0)=0;
y1(x1~=0)=0;
f = -mean(x1.*a)+mean(y1.*b);

2 Comments

Thanks for your response. I defined a and b vectors in another .m file. So, they should not be random vectors. They have specific values for each element of vector. I used ''mean'' in the function to get the expected value of the x1*a and y1*b vectors. Is there any other solution for calculating the expected value (E)?
Of course for your function they are not random, but to test this code, I needed input.
Here you see the importance of a well worded question. Do you need that filtering step, and if so, is this what you meant? As for that E, you really need to think how you would solve this on paper. What are x1 and y1? What are a and b? Do you need an element-wise product? What do you actually need to do mathematically.
If you don't know what your code should do, it is impossible to write good code for it.
Another hint: use comments. Try to explain for each line of code what it does and what you need to accomplish. This will make debugging much easier, as you're forced to think about each line. (and it helps others and future you understand and debug your code)

Sign in to comment.

Asked:

on 7 Jul 2017

Commented:

Rik
on 7 Jul 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!