Multiply each column of a matrix by another matrix - matlab

I have a M x N matrix. I want to multiply each of the N columns by a M x M matrix. The following does this in a loop, but I have no idea how to vectorize it.
u=repmat(sin(2*pi*f*t),[n 1]);
W = rand(n);
answer = size(u);
for i=1:size(u,2)
answer(:,i) = W*u(:,i);
end

You simply need to multiply the two matrices:
answer = W*u;
Think about it: in every iteration of your loop you multiply a matrix by a vector. The result of that operation is a vector, which you save into your answer in column i. Matrix multiplication is a similar thing: you can understand it as multiplication of a matrix (W) by a set of vectors, which form your matrix u.
So your code is good, just remove the loop :)

Related

Indexing 3d matrix with 2d Matrix plus vector

I have a m * n * k Matrix called M which I want to index to get the mean of some Data.
I have a logical m * n matrix called EZG and want to apply this on every of the k-th dimension from 1:(end-1) (call this vector V).
Any chance to write it without a loop like this:
M=rand(3,3,3)
EZG=logical([1,1,1;0,1,0;0,0,1])
V=1:size(M,3)-1
mean(mean(M(EZG,V)1),2)
Result should be a onedimensional vector of the length of V.
Thank you
I think this is what you want:
M=rand(3,3,3);
EZG=logical([1,1,1;0,1,0;0,0,1]);
% repeat EZG K-1 times, and add zeros to the Kth slice
V=cat(3,repmat(EZG,1,1,size(M,3)-1),false(size(M,1),size(M,2)));
% logical index and mean
m=mean(M(V));

How to raise a matrix to a vector of powers in matlab without for loop?

I have a 2x2 matrix that I want to multiply by itself 10 times while storing the result after each multiplication. This can easily be done with a for loop but I would like to vectorize it an eliminate the for loop. My approach was to have my 2x2 matrix a and raise it to a vector b with elements 1:10. The answer should be a 2x2x10 matrix that replicates typing
a^b(1)
a^b(2)
.
.
.
a^b(10)
To clarify I'm not doing this element wise I need actual matrix multiplication, and prefer not to use a for loop. Thanks for any help you can give me.
here is the code for you. I use the cellfun to do this and I have comments after each line of the code. It can compute and store from fisrt - nth order of the self-multiplication of an arbitrary matrix m. If you have any question, feel free to ask.
function m_powerCell = power_store(m, n) %m is your input matrix, n is the highest power you want to reach
n_mat = [1:n]; %set a vector for indexing each power result in cell
n_cell = mat2cell(n_mat,1,ones(1,n)); %set a cell for each of the power
m_powerCell = cellfun(#(x)power(m, x), n_cell, 'uni', 0); %compute the power of the matrix
end
%this code will return a cell to you, each element is a matrix, you can
%read each of the matrix by m_powerCell{x}, x represents the xth order

Extracting block diagonal from matrix

I have an njxnj matrix made up of nxn matrices. I want to extract the diagonal j blocks of nxn matrices. i.e. I want to extract the diagonal (for n = 2, j = 4):
What would be the most efficient way of doing this?
To index the elements you can use blkdiag to create a corresponding mask.
%your parameters
n=2
j=4
%some example matrix
M=magic(n*j);
%create the input for blkdiag, j matrices of size n
h=repmat({true(n)},j,1)
%use blkdiag to select the elements
M(logical(blkdiag(h{:})))
For large j, this answer of #Daniel becomes slow. I would instead recommend using linear indices of block diagonal.
n=2;
j=4;
%some example matrix
M=magic(n*j);
linIndices = (0:n*((n*j)+1):n*((n*j)+1)*(j-1))+reshape((1:n)'+n*j*(0:n-1),[],1);
newM = reshape(M(linIndices),n,n,[]);

Matlab: multiplying rows of a matrix by vector elements

Let v be a row vector (1 x n matrix) and M be a n x m matrix.
I use the following piece of code to create a "weighted vector" (I hope the comments explain what it's supposed to be doing):
weighted_M = bsxfun(#times,v',M);
%creates a matrix with the i-th row of M being weighted (multiplied) by the i-th element of v
weighted_v = sum(weighted_M);
%sums the columns of weighted_M
Now the actual question: I have to do the same calculation for a lot of input vectors v. So instead I would like to input a matrix V that contains the vectors v as rows and output a matrix that contains the weighted vectors as rows. Is there any way to do this without using for loops?
If V is of size [k,n] and M is of size [n,m], and you're looking for the k weighted vectors, then you might simply need
weighted_vs = V*M;
an element of which is equal to
weighted_vs_ij = (V*M)ij = sum_l V_il * M_lj
First you multiply each row of M with a corresponding element of V (V_il * M_lj above for a fix i), then sum up as a function of the first index.
The result are the k weighted row vectors, each of length m.

Mean of rows and columns of matrices in a cell

I have say n a x b matrices and I want to generate a new matrix of dimension a x b which is the mean of all n a x b matrices, i.e the first element of this new matrix is the mean of all first elements in each n a x b matrices and so on. Is there a way to compute this average matrix from a group of matrices in MATLAB? I had tried to do this by creating a cell but couldn't figure out how to take mean of each element of these matrices. I would appreciate any ideas or suggestions.
First, put your n matrix in a single axbxn matrix
M = cat(3, mat1, mat2, mat3, ...);
Or, if you work with a cell array,
M = cat(3, cellOfMats{:})
Then just use mean along the third dimension
meanmat = mean(M,3)