Multiplication of Sparse matrix on Matlab - 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?

Related

Error using multiplication matlab

I am trying to plot this function in matlab but I get the error: Error using *
Inner matrix dimensions must agree
Why is this happening?
My code:
H_s=2;
f_zero=2;
f=0:0.001:0.01;
S_f=(5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5)*exp(-(5/4)*f)
plot(f,S_f)
The first terms ((5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5)) evaluate to a 1x11 matrix, as does the last term (exp(-(5/4)*f)). The matrix multiplication operator * indeed requires the inner dimensions to match.
But you were probably trying to do element-wise multiplication .*:
S_f=(5*(H_s).^2)/(16*f_zero)*(f/f_zero).^(-5).*exp(-(5/4)*f)

Calculating large number of generalized eigenvalues with Matlab on GPU

I have a lot of small matrices already stored on GPU card and now I want to calculate their generalized eigenvalues with another matrix.
Current code:
cov=gpuArray(cov) %7x7 matrix;
p=1:numel(ix1); %numel(ix1)... number of stored matrices
p=gpuArray(p);
%covs... 7x7xn matrix already on GPU, n is large (>100000)
g = arrayfun(#(x) eig(covs(:,:,x)/cov), p);
If I tried to run code I get error that function eig is unsupported, but I read that eig is supported on GPU.
So my question is what I did wrong (my first attempt with arrayfun) and if there is better way to calculate generalized eigenvalues.

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?

Matlab Question on Sparse Matrices

I have a sparse matrix S.
I perform the following operation
D1 = diag(sum(S,2)), basically forming a diagonal matrix.
Now I need to perform (D1)^(-0.5), but I get an error
"Error using mpower, use full(x)^full(y)"
Converting to full will defeat the purpose of using a sparse matrix.
Any advice will be very helpful.
Raising a diagonal matrix to a power can be done simply by doing the operation on the diagonal elements elementwise... so:
D1_diagonal_elements = sum(S,2);
your_result = diag(D1_diagonal_elements .^ (-0.5));