Error using + Matrix dimensions must agree. in Matlab - matlab

I seem to have a really stupid problem but I cannot figure it out.
I have this expression in matlab
ref_phys = #(x,y, csi) B(x,y) * csi + b(x,y);
On my laptop everything is fine, but moving it into another system which runs a 2016 version of Matlab, I get
Error using +
Matrix dimensions must agree.
Error in L2_proj_2D>#(x,y,csi)B(x,y)*csi+b(x,y) (line 39)
ref_phys = #(x,y, csi) B(x,y) * csi + b(x,y);
And since nothing has changed from my laptop code, I'm 100% sure that the matrix dimenions agree.
B(x,y) has dymension 2-by-2
csi has dimension 2-by-3
b(x,y) has dimension 2-by-1
Thus everything should give a 2-by-3 matrix
Could it be the version of matlab? How can I solve it ?
Thanks!

Related

Image reconstruction from a sinogram via SVD, in MATLAB

I've been tasked with reconstructing a 256x256 image from a 367x180 sinogram in MATLAB, using SVD.
I ran the SVD no problem using [U,S,V] = svd(true_sinogram);
This produces U (367 x 367), S (367 x 180), and V (180 x 180). My issue is that I'm not sure how to reconstruct the original image (not the sinogram) from this.
I've attempted the pseudo-inverse by A = V.*S_recip.*U';, where U' is the transpose of U and S_recip is the reciprocal of S, but I'm getting a "Matrix dimensions must agree" error.
Simple multiplcation of the original elements just results in the original true sinogram, which I can only back-project. Truncating the matrices to 180x180 also doesn't work.
Has anyone got any ideas? I feel I'm missing something obvious. Thank you!
Not mere reciprocals of S but instead its transpose should be used to find pseudoinverse.
For example, if A=U*S*V' then the pseudoinverse of A would be equal to V*S_recip'*U', where S_recip' is the transpose of S_recip, and would have its size as 180 x 367.

Matrix inversion is difficult in matlab when deal with sparse matrix

I implement a algorithm which is related to sparse matrix inversion.
The code:
kapa_t=phi_t*F_x'*(inv(inv(R_t)+F_x*phi_t*F_x'))*F_x*phi_t;
I write down the code in matlab. It give me a warning Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.419037e-18.. But as per my algorithm matrix inversion is important part. So, I am trying to search some efficient way for matrix inversion.So I find out this link how to compute inverse of a matrix accurately?
So I changed my code as suggest.
kapa_t=phi_t*F_x'*(inv(inv(R_t)+F_x*phi_t*F_x'))\F_x*phi_t;
After that I get an error Error using \
Matrix dimensions must agree.
Error in EKF_SLAM_known (line 105)
kapa_t=phi_tF_x'(inv(inv(R_t)+F_xphi_tF_x'))\F_x*phi_t;
The algorithm I am using is
Here line no: 8 of the algorithm is equivalent to code kapa_t=phi_tF_x'(inv(inv(R_t)+F_xphi_tF_x'))F_xphi_t;
What should I do with my code to get rid of this warning.
kapa_t=phi_t*F_x'*(inv(inv(R_t)+F_x*phi_t*F_x'))\F_x*phi_t;
should be
kapa_t=phi_t*F_x'*((inv(R_t)+F_x*phi_t*F_x')\F_x)*phi_t;
The A \ B operator is roughly equivalent to inv(A) * B when A is square, so you don't need the outer inv.

Error when using cellfun to do regression on a cluster

I'm using Matlab 2016a on my university's cluster. My local PC has Matlab 2016b installed.
I'm attempting to do a very large regression using cellfun. The drought resistance and predictor layers are 2044x1572. The internal dimensions of "drought resistance" is 100x1 while the internal dimensions of "predictors" is 100x6, representing the different predictors for each cell.
d_regress=cellfun(#(x,y)regress(x,y),Drought_resistance,predictors,'UniformOutput',false);
When I run this code with sample data on my local PC I get no errors.
However, when I run this code on the cluster I get the error:
Error using *
Inner matrix dimensions must agree.
Error in regress (line 93)
b(perm) = R \ (Q'*y);
Error in #(x,y)regress(x,y)
So then I fix the inner matrix dimensions so that they are 1x100 and 100x6.
Then I get the following error, which makes sense because of how regress works.
Error using |
Matrix dimensions must agree.
Error in regress (line 66)
wasnan = (isnan(y) | any(isnan(X),2));
Error in #(x,y)regress(x,y)
I've been having to use mldivide on the cluster in order to do my regressions (mldivide gives me no errors), but I would prefer to use regress due to its additional functionality. Is there any way I can modify my code in order to use regress?

Error using ==> mtimes MATLAB

I am trying to numerically integrate a function on MATLAB (I am on MATLAB 7.7). Here is my code.
fun = #(t)(cos(t)./sqrt(3)-sin(t)).^39*(cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2 + 2/3*sin(t).^2);
quad(fun,-pi/6,pi/6)
Unfortunately I am not following the error message. Can anyone help me please?
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==>
#(t)(cos(t)./sqrt(3)-sin(t)).^39*cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2+2/3*sin(t).^2)
Error in ==> quad at 77
y = f(x, varargin{:});
I have tried to see if the function definition is right. It seems to work for me:
fun(1)
ans =
-1.4078e-007
which the correct value I would expect when evaluated at 1. I have done several trials with various inputs, the function fun() seems to compute them alright!
p.s.: I have used quad() before this. It has worked well for me previously.
Your function works for scalar inputs, but not for vectors:
>> fun(1:5)
Error using *
Inner matrix dimensions must agree.
You need to change your function to do elementwise multiplication:
fun = #(t)(cos(t)./sqrt(3)-sin(t)).^39.*(cos(t)./sqrt(3)+sin(t)).^63.*sqrt(2*cos(t).^2 + 2/3.*sin(t).^2);

SciLab matrix assignment Invalid Index Error

I want to hide an image inside an big image using SciLab tools, following is the code snippet I am using
S1_diag = diag(s1);
S2_diag = diag(s2);
S1_diag(1:length(s1), :) = S2_diag(1:length(s1), :);
where s1 and s2 are image 1 and 2's singular diagonal matrix
The same code works in Matlab but generates an 'Invalid Index' error (21) in SciLab. What I am missing?
I am novice in SciLab syntax so couldn't understand how to address this in SciLab.
Any help is appreciated.
The reason is that the length command is not the same for Scilab and Matlab.
in Matlab, length gives the maximum dimension of a matrix. So, for a 2-by-3 matrix it is 3.
in Scilab, length gives the number of elements. So, for a 2-by-3 matrix it is 6.
Here's a little dictionary:
Matlab's length(A) is the same as Scilab's max(size(A))
Scilab's length(A) is the same as Matlab's numel(A)