SciLab matrix assignment Invalid Index Error - matlab

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)

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.

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.

Matlab sparse and dense matrix concatenation mismatch

I have the following problem in MATLAB.
d is a dense matrix dimensions c,t
f is a dense matrix dimensions u,p
p is a cell array (dimension p) in which each cell contains a logical sparse matrix of indexes of dimensions c,t
I want to execute the following instruction:
for r=1:u
f(r,:) = mean(mean(d(p{:})))
end
I noted that if p contains full logical matrices the command works, but since they are sparse it isn't working. It gives me the error: "matrix dimensions must agree".
I know that the : syntax concatenates the content of an array, and I know that I can circumvent it with another for cycle.
Does anyone knows how to execute this command without an additional for cycle?

Weird behavior of a sparse Matrix under MATLAB

I have been given this 63521x63521 real sparse symmetric matrix in MATLAB and for some reason it seems to be behaving weirdly for some commands.
I am not sure if there is a 'defect' in the matrix file or in the way I am using
MATLAB's commands.
Consider the following script. I have indicated the output of each of the steps.
% Gives sparsity shown as expected, so this works fine
spy(rYbus)
% I want the top 3 singular values of rYbus. But this line Returns empty matrix! Why/
S = svds(rYbus,3);
% Set exact answer and rhs and solve the linear system with iterative and direct method
b_exact = ones(size(Ybus,1),1);
rhs = rYbus*b_exact ;
% Following line gives Warning: Matrix is singular, close to singular or badly scaled.
% Results may be inaccurate. RCOND = NaN.
% > In Ybustest at 14.
b_numerical_1 = rYbus\rhs;
% Even for a single GMRES iteration b_numerical_2 is a vector of Nans. Why?
b_numerical_2 = gmres(rYbus,rhs,[],[],1);
Can anyone point out what may have gone wrong?
I have already used the "isnan" function to verify that the matrix rYbus
does not have any nans. The size of the matrix is 63521 x 63521
Have you checked if your input sparse matrix rYbus has any NaNs? If I remember correctly, svds can give you an empty matrix instead of an error.
Another possible error is the size of rYbus. What is the size of it?

Cryptic matlab error when computing eigs

I am trying to find the 2 eignevectors of the 2 smallest eigenvalues of a laplacian. I do this by
[v,c]=eigs(L,M,2,'SM');
Where L is the lapalcian and M is the mass matrix.
As a result I get the error
Error using eigs/checkInputs/LUfactorAminusSigmaB (line 1041)
The shifted operator is singular. The shift is an eigenvalue.
Try to use some other shift please.
Error in eigs/checkInputs (line 855)
[L,U,pp,qq,dgAsB] = LUfactorAminusSigmaB;
Error in eigs (line 94)
[A,Amatrix,isrealprob,issymA,n,B,classAB,k,eigs_sigma,whch, ...
Does this mean I am doing something wrong, or is this just matlab choosing a bad initial guess for its iteration process?
The matrices I am using should have a descent condition number...
I ran into the same problem while implementing normalized cuts segmentation. The condition number is actually infinite because the smallest eigenvalue is 0, and this is basically what MATLAB's error message is about. It's running LU decomposition first.
I just added a multiple of I, 10*eps*speye, to the normalized Laplacian to improve conditioning and that fixed it.
I had the same problem with the eigs function. So I went the long (and maybe stupid) way but it did the job for me as my problem is not that big: (I will try to keep your notation)
% Solve the eigenvalue problem using the full matrices
[v,c]=eig(full(L),full(M));
% Sort out the eigenvalues using the sort function (the "-" sign is because you want the smallest real eigenvalues in magnitude)
[E,P] = sort(real(c),'descend'); % Here I am assuming you know all the eigenvalues have` negative real parts
% Now P is a vector that contains (in order) the indices of the row permutation operated by the % function sort.
% In order to obtain the two eigenvectors corresponding to the 2 smallest eigenvalues:
for k = 1:2
index = P(k);
lambda(k) = c(index,index); % use this to check if c(index,index)=E(k,k)
eigvec(:,k) = v(:,index); % corresponding eigenvector
end
Hope this helps
G