Solving a sparse matrix using \ in matlab - matlab

I am trying to solve a problem of the form Ax=b in which I have a tridiagonal matrix A, and a full vector b.
When doing x=A\b I get the error message:
Warning: Matrix is close to singular or badly scaled. Results may be
inaccurate. RCOND = 3.301735e-150.
I have theorised that this may be due to the sparsity of matrix A, is there a more efficient built in way of dealing with this in Matlab?

Related

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.

inverse() vs pinv() which one good for linear system

I have a linear system A*x=b. Here x is the unknown value, so I have to solve for x. A is a sparse matrix whose diagonal and off-diagonal elements have some non-zero values. The other elements is zero or close to zero.
Using MATLAB, I have two options
x = inv(A) * b
x = A \ b
But both give me NaNs in the result. I know this will happen because A is a sparse matrix. So, I tried pinv() which is pseudo-inverse. This time I got some results.
Is it OK to use pseudo-inverse in a situation like this, where inverse has failed to produce a result? What type of result does pseudo inverse produce? Is it reliable or it is a form of error?

How to multiply matrix of nxm with matrix nxmxp different dimensions in matlab

In my current analysis, I am trying to multiply a matrix (flm), of dimension nxm, with the inverse of a matrix nxmxp, and then use this result to multiply it by the inverse of the matrix (flm).
I was trying using the following code:
flm = repmat(Data.fm.flm(chan,:),[1 1 morder]); %chan -> is a vector 1by3
A = (flm(:,:,:)/A_inv(:,:,:))/flm(:,:,:);
However. due to the problem of dimensions, I am getting the following error message:
Error using ==> mrdivide
Inputs must be 2-D, or at least one
input must be scalar.
To compute elementwise RDIVIDE, use
RDIVIDE (./) instead.
I have no idea on how to proceed without using a for loop, so anyone as any suggestion?
I think you are looking for a way to conveniently multiply matrices when one is of higher dimensionality than the other. In that case you can use bxsfun to automatically 'expand' the smaller matrix.
x = rand(3,4);
y = rand(3,4,5);
bsxfun(#times,x,y)
It is quite simple, and very efficient.
Make sure to check out doc bsxfun for more examples.

Multiplication of Sparse matrix on Matlab

I am using Matlab R2011a. I have a matrix A which is of size 27*3355432. This matrix has a lot of zeros. I need to compute the comand eig(A'*A) but I can't even do the A'*A. I have tried using the sparse matrix B = sparse(A)and then computing B'*B but I get the error:
??? Error using ==> mtimes
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead
Truth is I am not a Matlab expert. Is there a way to produce such a database?

Matlab inverse issue - fmri data - partial correlation algorithm

I'm using the following code To get a partial correlation matrix (original code from http://www.fmrib.ox.ac.uk/analysis/netsim/)
ic=-inv(cov(ts1)); % raw negative inverse covariance matrix
r=(ic ./ repmat(sqrt(diag(ic)),1,Nnodes)) ./ repmat(sqrt(diag(ic))',Nnodes,1); % use diagonal to get normalised coefficients
r=r+eye(Nnodes); % remove diagonal
My original matrix (ts1) is a brain activity over time course (X variable) in multiple voxels -volumetric pixel 3X3 (Y variable).
The problem is, I have more dependent variables(y -voxels ) than independent variables(x - time course).
I get the following Warning-
Warning: Matrix is close to singular or badly scaled.
Results may be inaccurate. RCOND = 4.998365e-022.
Any thoughts on how to fix the code so I'll get the partial correlation between all of the voxels?
The warning is from Matlab having a problem inverting the covariance matrix.
One solution might be to try pinv()
http://www.mathworks.com/help/techdoc/ref/pinv.html