Matrix with the matrix elements - matlab

I want to create a matrix in large dimensions that the components themselves are a matrix.
Like the following example
each of the W, V, U is 18*18 matrix and the other components are zero. What is the easiest way to create such a matrix in MATLAB?

Assuming that you want a matrix that contains n x n blocks so its dimensions will be (18 * n) x (18 * n):
n=10;
z=ones(n,1);
result = kron(spdiags(z,-1,n,n),V)+kron(spdiags(z,0,n,n),U)+kron(spdiags(z,1,n,n),W);

Related

Dividing matrix values by the size of the matrix produces error

I have a matrix 1080x1920 double. I want to divide the matrix by it's size. However, it returns an error.
[n m] = size(a);
a/[n m];
**Error using /
Matrix dimensions must agree.**
Any help is appreciated.
When you use [n m]=size(a), n is the number of line and m is the number of column (or row) : then [n m] is a line-matrix with 2 elements.
If I'm right, you're trying to divide a by the number of element in a. So wheter you do a/(n*m) or you can use too the function length since n*m=length(a) (the length function gives you the number of element in the input matrix.
Otherwise, if you're trying to do the matrix multiplication a/[n m] or a*([n m]^-1), the dimension of the matrices a and [n m] have to be mathematically consistent to perform such a matrix multiplication.

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));

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)

MATLAB - resizing matrix using matrix multiplication and not the RESIZE command

For a specific problem, I need to design the resizing of a matrix process using multiplication of matrices alone.
Given a matrix of A of dimensions (a*b,1) where a and b are integers, I need to find a way to resize A to dimensions (a,b) like this:
M*A*N = resize(A,a,b)
where dim(M) = (a,a*b) and dim(N) = (1,b). It doesn't have to be two matrices but I don't think it is possible any other way.
If you can't use reshape or vec2mat, you need to do your manipulation for each element of A separately.
There is no such M and N that you are searching for.
Suppose:
resh_A = M*A*N;
Let's study one row of this equation. Assume one row of M*A :
temp_i = M(i, :) * A;
Since M(i, :) is 1 x a*b and A is a*b x 1; temp_i whould be a 1 x 1 matrix.
Now temp_i * N should result in the ith row of your result (or resh_A).
Thus resh_A will look like:
(note N is 1 x b)
temp_1 * N % row1
temp_2 * N % row2
temp_3 * N % row3
...
which is not a general matrix (it's a matrix with rank 1).