## Inner matrix dimensions must agree.

### Sandro Navarro (view profile)

on 25 Mar 2015
Latest activity Commented on by Mike Hosea

### Mike Hosea (view profile)

on 6 May 2015
We have 3 functions H, U and D which depend on two variables, x and P. To be more precise, only H depends on both x and P, both U and D depend only on x. Also, we forget about the other parameters t,T,L,J and S for now, to simplify the statement of our problem. Finally, we clarify that x is a vector of L+1 variables.
We wish to consider the product function I(x,P)= H(x,P)*U(x)*D(x) and to evaluate the integral of I with respect to x, yielding a function V which depends only on P.
Our code for V is the following:
function V = Vvector(P,y,t,T,L,J,S) %Calcula v(t) de la Proposición 3.4
H =@(b) MC1(b,y,L,T,J,P); % Calcula H_T de la Proposición 3.4
U =@(a) Ufunction(a,y,t,T,L,S); % Calcula Uh_t de la Proposición 3.4
D =@(c) densityfunction(c); % Es la función pi de la Proposición 3.4
I =@(x) U(x)*H(x)*D(x);
V = integral(I,-Inf,Inf);
However, when we run the program we get the following error message:
Inner matrix dimensions must agree.
Error in MCfunction (line 6)
hv(j) = exp(1i*(x*y(j:j+L)));
Error in MC1>@(x)MCfunction(x,y,T,L) (line 5)
Hobs= @(x) MCfunction(x,y,T,L);
Error in MC1 (line 8)
H = Hobs(x) - Hsim(x);
Error in Vvector>@(b)MC1(b,y,L,T,J,P) (line 3)
H =@(b) MC1(b,y,L,T,J,P); % Calcula H_T de la Proposición 3.4
Error in Vvector>@(x)U(x)*H(x)*D(x) (line 9)
I =@(x) U(x)*H(x)*D(x);
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 104)
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in Vvector (line 11)
V = integral(I,-Inf,Inf);
Apparently, there exists some problem when computing the H function, despite the fact that when implemented separately there are no errors reported and everything seems to work fine (for all three functions).
We are wondering what is wrong with our code. Perhaps, something is wrong with the definition of I, but we cannot figure it out. We repeat that there are no errors reported when running the H, U and D functions separately.

### Mike Hosea (view profile)

on 25 Mar 2015
Edited by Mike Hosea

### Mike Hosea (view profile)

on 25 Mar 2015

You wrote
I =@(x) U(x)*H(x)*D(x);
That is probably not right. The * operator is matrix multiplication. You probably meant
I =@(x) U(x).*H(x).*D(x);
i.e., element-wise array multiplication. Note that from the documentation for INTEGRAL, the integrand must be able to accept array inputs and return array outputs. That is to say, INTEGRAL evaluates the function at multiple x values at once, just as would happen if you were, say, to run plot(x,I(x)), where x is some vector, e.g. x = linspace(-10,10). In fact, if you make this work for PLOT, you'll probably be good to go for INTEGRAL. Evaluation is vectorized for performance reasons, and the benefits can be quite significant.
Now, if U, H, and D are also implemented without regard to processing vector inputs, then it may be a fair bit of work to make them right. Often, you can just change matrix multiplication to element-wise multiplication, matrix exponentiation ^ to element-wise exponentiation .^, and matrix "division" / to element-wise division ./. If so, that option will probably yield the best performance. If your functions use control-flow logic (e.g. if/else/end), then you will probably find it easiest to get up-and-running if you use ARRAYFUN. For example, if I(x) evaluates correctly without errors for all scalar values of x, then
I1 = @(x)arrayfun(I,x)
V = integral(I1,-Inf,Inf);
Finally, I am assuming V is to be a scalar here, i.e. I(x) returns a scalar value when x is a scalar. If it is a vector-valued integrand, i.e. I(x) returns a vector with more than one element when x is a scalar, then INTEGRAL provides the 'ArrayValued' option:
V = integral(I,-Inf,Inf,'ArrayValued',true);
In this case you don't need to vectorize the integrand, and in fact, with a vector-valued integrand, you might actually intend to use matrix operations instead of element-wise operations. Since scalar valued problems can be viewed as vector-valued problems as well, you can use 'ArrayValued',true as a quick and dirty way of getting around a vectorization issue, but it is usually slower than using ARRAYFUN on scalar-valued integrands. ### Andrew Newell (view profile)

on 25 Mar 2015

Note the top error statement:
Inner matrix dimensions must agree.
Have you tried testing these functions with vector arguments?

studentU

### studentU (view profile)

on 18 Apr 2015
hi,
I have "inner matrix dimensions must agree" problem, to multiplication of these matrix: A [64x1] and B [6x1], if I make a multiplication element by element: "size mismatch" :/, yet his works very well on the command window, do you have to remedy proposal?
Mike Hosea

### Mike Hosea (view profile)

on 6 May 2015
That doesn't work in the command window, either.
>> A = zeros(64,1);
>> B = zeros(6,1);
>> A*B
Error using *
Inner matrix dimensions must agree.
>> A.*B
Matrix dimensions must agree.
Nor does it make any mathematical sense. I think your problem is not yet formulated well enough in MATLAB code that we can just help you debug a function call. If you're talking about multiplying 64-by-1 and 6-by-1 vectors and expecting that to work in any sense, I wouldn't even know where to begin. Maybe you want KRON, say kron(A,B')?