Matlab ??? Error using ==> mldivide Matrix dimensions must agree - matlab

I have codes like these in Matlab
...
Kx=cell(10,10,10);
Kx2=cell(ee_max)
bX=cell(10,10)
bX2=cell(ee_max)
...
Kx{ee,ii,jj}=(1/36*VX{ee})*(alphX{ee,x}*bX{ee,ii}*bX{ee,jj}+alphX{ee,y}*cX{ee,ii}*cX{ee,jj}+alphX{ee,z}*dX{ee,ii}*dX{ee,jj})+VX{ee}*1/20*betaX{1+delXX{ii,jj}};
bX{ee,ii}=VX{ee}*(1/4)*f{ee}
...
Kx2{ee}=Kx{ee,:};
bX2{ee}=bX{ee,:};
phi_next=cell(4,4);
...
phi_next{ee}=bX2{ee}/Kx2{ee}
....
I got error at this line
phi_next{ee}=bX2{ee}/Kx2{ee}
as
??? Error using ==> mldivide
Matrix dimensions must agree.
so,what should I do?
regards

Use ./ instead of /.
The code is not complete so you may have other errors with other matrices dimensions.
If you find other errors you may use .* instead of * also when you multiply matrices with different dimensions.
Example:
A: 3 rows, 4 columns
B: 2 rows, 4 columns
C: 4 rows, 1 column
You can use * to multiply A and C (3x4)(4x1) or B and C (2x4)(4x1).
However you need .* to multiply A and B (3x4)(2x1).

Related

Matlab error for matrix dimensions

I get an error
Error using -
Matrix dimensions must agree.
Error in Untitled6 (line 32)
temp=double(S_bar) - (repmat(mean_face, 1, num_images));
code :
for i=1:num_images
[m,n] = size(S(:,i))
[a,b] = size(repmat(mean_face, 1, num_images))
temp=double(S(:,i)) - (repmat(mean_face, 1, num_images));
size of S(:,i) is [45045 1] and size of repmat(mean_face, 1, num_images is [45045 45]
So i tried to use the transpose of S(:,i) so the dimensions become [1 45045] so that i can get a resulting matrix of [1 45]. But even when i use the transpose i get the same error. Why am i getting the error even though the matrix dimensions are correct?
You need to do what i believe is called "broadcasting", i.e. making sure elementwise operations on matrices are done on matrices of compatible sizes. One elegant way of doing this is using bsxfun :
temp=bsxfun(#minus,double(S(:,i)),(repmat(mean_face, 1, num_images)));
You could also use repmat on your double(S(:,i)) so that it has the right number of columns.
Hope this helps,
Tepp

bsxfun: Dimensions of matrices being concatenated are not consistent

Any one knows where the error is? Many thanks!
beta=randn(50,1);
bsxfun(#(x1,x2) max([x1 x2 x1+x2]), beta, beta')
error message:
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in #(x1,x2)max([x1,x2,x1+x2])
I'm not 100% sure what you want to achieve, but the error is in the transposition of beta as third argument of bsxfun; it works like this:
beta=randn(50,1);
bsxfun(#(x1,x2) max([x1 x2 x1+x2]), beta, beta)
The second and third argument of bsxfun needs to be of the same size to apply element-wise binary operations to it.
Edit: From the manual (http://www.mathworks.de/de/help/matlab/ref/bsxfun.html):
fun can also be a handle to any binary element-wise function not
listed above. A binary element-wise function of the form C = fun(A,B)
accepts arrays A and B of arbitrary, but equal size and returns output
of the same size. Each element in the output array C is the result of
an operation on the corresponding elements of A and B only.
EDIT2: Is this, what you want?
A = rand(1,50);
[x, y] = ndgrid(1:length(A), 1:length(A));
idc = [x(:) y(:)];
allMin = min([A(idc(:,1)) A(idc(:,2)) A(idc(:,1))+A(idc(:,2))]);
First, with the second and third code-line I generate all possible combinations of indices (all pairs i/j), e.g.: If A has 3 entries, idc would look like:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
and then I'm building up a vector containing the value A(i), A(j) and A(i)+A(j) for each row of entries (i, j) and getting the min of it.
Here's what I got (using two max in bsxfun)
beta = randn(50,1);
res = bsxfun(#(x,y) max( x, max(y, x+y) ), beta, beta');
Verifying using repmat
tmp = max( cat(3, repmat(beta,[1 50]), repmat(beta',[50 1]), ...
repmat(beta,[1 50])+ repmat(beta',[50 1]) ), [], 3);
isequal( tmp, res )

How do I put in two variables in a matlab function (ERROR: inner matrix dimensions must agree)?

When I try to plot a function h in MATLAB, using a variable omega which is defined as its own function, I get an Inner matrix dimensions must agree, error using _*_ response from the console.
The function works when I use a + between the seperate function-components of h; It does not work when I try multiplying the two inner functions in h, which is, from what I guess, what causes the matrix dim error.
function h = freqp(omega)
k = (1:1024-1);
hh = (1:1024-1);
omega = zeros(length(k),1);
omega = (k-1)*((2*pi)/1024);
hh = 2*exp((-3j)*omega)*cos(omega); % This works for ...omega) + cos(...
% but not for ...omega) * cos(, why?
y = fft(hh);
stem(real(y), omega);
How can I solve this? I read the info on mathworks but it only gives a solution for e.g. loading a file. Any help would be greatly appreciated!
Since Omega is a vector, the addition works. But multiplication of two vectors will result as a matrix. You can modify
hh = 2*exp((-3j)*omega)*cos(omega);
as
hh = 2*exp((-3j)*omega)*(cos(omega))';
or looking for element wise multiplication,
use
hh = 2*exp((-3j)*omega).*cos(omega);
The part exp((-3j)*omega worked fine because -3j is a complex scalar and omega a vector. Thus, MATLAB multiplies each element of omega with -3i. However, that result is a vector itself. Also cos(omega) is a vector, and both are row vectors.
In this case, with two vectors, the *-operator means dot product but that would be calculated between a column vector and a row vector, not two row vectors. So, [1 2 3] * [4 5 6] will raise the same error you are reporting, but [1 2 3] * [4 5 6]' yields 32.
From invoking fft on hh your code looks, however, as if you never intended to calculate a dot product (a scalar) but instead were looking for element-wise multiplication. The operator for element-wise multiplication is .*, such that your expression would be instead
hh = 2*exp((-3j)*omega).*cos(omega);

Multiply a 180x360 array by a 156-item time-series to get 180x360x156 array?

bsxfun(#times,RegressIndexFlux.(IndexNames{i}).(FluxNames{j}), Indices.(IndexNames{i}));
So my code is above.
The problem with bsxfun is that I get the below error message:
Error using bsxfun
Non-singleton dimensions of
the two input arrays must
match each other.
So here's the question: is there a way for me to convert the 180x360 array by a timeseries without having to use for loops, if possible? (I'm using many structures of 180x360 arrays here). Basically RegressIndexFlux is regressed against the time-series Indices, and I'm trying to get a reconstruction of the time-series by only using the regression.
You can't have mismatched non-singleton dimensions. Permute the second argument. Assuming A is a m-by-n matrix and B is a (p-by-1) column vector:
A = rand(6,5); B = rand(4,1);
% m-by-n #times 1-by-1-by-p => m-by-n-by-p
C = bsxfun(#times,A,permute(B,[3 2 1]));
size(C)
ans =
6 5 4

Why the valid looking statement gives error in MATLAB?

It's from this question。
Why the two solutions doesn't work, though it looks very valid for me:
>> t = -pi:0.1:pi;
>> r = ((sin(t)*sqrt(cos(t)))*(sin(t) + (7/5))^(-1)) - 2*sin(t) + 2 ;
??? Error using ==> mtimes
Inner matrix dimensions must agree.
>> t = -pi:0.1:pi;
>> r = ((sin(t).*sqrt(cos(t))).*(sin(t) + (7/5)).^(-1)) - 2*sin(t) + 2 ;
>> plot(r,t)
??? Error using ==> plot
Vectors must be the same lengths.
What's wrong with the above?
The * operator is the matrix multiplication operator, which requires its operands to have matching inner matrix dimensions. The .* operator is the element-wise multiplication operator, which requires its operands to have the same size (or for one to be a scalar) so it can perform multiplication on each matching pair of elements. See this link for more detail.
Also, I don't get the plotting error you do when I run the second solution. I just get this warning:
Warning: Imaginary parts of complex X and/or Y arguments ignored