Create a matrix from a sub matrix? - matlab

I have a matrix of size N -by- N and I want to create a matrix (10 N) -by- (10 N), where each N -by- N block is a copy of the original matrix. I would like to do it without for loops.
I have tried with the function kron, but it only "enlarges" the original matrix.
How can I make this matrix?

You're looking for repmat()
A = rand(10); % Original 10 x 10 matrix
B = repmat(A, 10); % Copied 10 times in each direction

Related

How can I multiply a matrix with vector and reshape it using one matrix multiplication using matlab

I have a matrix X with size N x N, and a vector Y with size NM x 1. I want to multiply the matrix X with every N elements in Y and then reshape the resultant matrix Z row-wise. In other words, I first reshape the vector Y into a matrix Y_2 with size N x M . Then, get the matrix Z = X * Y_2, and finally reshape the matrix Z row-wise.
That process, I want to do it in matlab using matrices multiplications, as follows:
clear all; clc; clear;
N = 4; M = 8;
X = randn(N,N);
Y = randn(N*M,1);
Z = kron(eye(M,M),X) * Y;
The problem, is that I don't get $Z$ similar to that process explained above. I mean the result of the way I used in Matlab code is not similar to the results of the process explained before. how can I do it, where is it error in my other method?

For n = 2 ... 11 set up Vandermonde matrix A of size m Ɨ n and for each n given data set a. Matlab

I am given set of 50 data points with values {a^(i),b^(i)} for i=1,...,50
stored in the arrays a and b.
I know that the Vandermonde matrix A has size m x n, where n = 2 ... 11 and m is the size of the array a.
I want to to fit the data with a polynomial of degree (n āˆ’ 1), for n = 2,...,11. To do that for each n I have to set up the Vandermonde matrix A of size m Ɨ n.
The Vandermonde matrix A solves the following equation:
A^T*A*x = A^T*b
Where the A^T is the transpose matrix and I have b already given.
Also we know that Aij = (a^(i))^(jāˆ’1) for j = 1,...,n,
What confuses me is how to set the matrix for n = 2,..,11.
What my line of thought is:
I have m = length(a); this will set up m = 50;
n = 11;
Then A=ones(m,n); This creates a matrix A filled with ones that has the correct size.
However I am not sure how to populate the matrix.
I wrote the following for loop which I thought will populate the matrix:
for n = 2:11
j=n;
for i = 1:50
A(i,n) = (a^(i))^(j-1);
end
end
Could you help me please with setting up the matrix?
You should use the vander function. However, vander will return an m x m matrix, which is usually used to fit the data to a polynomial of degree (m-1). Since you want to fit to a polynomial of degree (n-1), you only need the last n columns of that matrix.
Here's the code:
A = vander(a);
A = A(:,end-n+1:end);

Discatenate a column vector to get back to its original square matrix in MATLAB

I had to convert an n x n matrix to n^2 x 1 column vector for ease of some operations. Now, that the operations are done, how do I return to the n x n form from the n^2 x 1 vector.
It is supposed to be opposite of this: concatenation
Thanks!
You can use the reshape() function:
//M is your n^2 x 1 column vector, A is your nxn matrix that you want to recover
A = reshape(M, [n n])
If your n x n matrix is 3x3, then:
A = reshape(M, [3 3])
For more info: http://www.mathworks.com/help/matlab/ref/reshape.html

MATLAB: Copy the first n elements of a vector, then skip n elements, then copy the next n elements

Vector x is 2000-by-1
I would like to take the first 20 elements of vector x and copy them to vector y, then copy the next 20 elements of vector x to vector z, then copy the next 20 elements to vector y, and so on.
I understand I could do this with a loop, but am hoping to find a more efficient method.
This can be achieved by reshaping the vector to a matrix, selecting odd/even columns and finally flattening the matrix:
m = reshape(a, 20, []);
x = m(:,1:2:end); x = x(:);
z = m(:,2:2:end); z = z(:);

Using a diagonal values in a square matrix? to avoid loop

I have 2 (1 x n) matrices that I want to multiply each element together, n number of times, shifting the second matrix 1 step each time.
This code works fine, but I'm wondering if a better way with (n by n) matrix, values on the diagonals and no 'for loop' would be faster (more elegant) ?
y = [];
for i=[1:length(x1)]
x2 = circshift(x2,[0,1]);
y(i) = sum(x1 .* x2);
end;
You can use convolution to obtain the same result as your code:
y = conv(x1, fliplr([x2 x2]), 'same');