How can I multiply a matrix with vector and reshape it using one matrix multiplication using matlab - 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?

Related

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

MATLAB: summing out one variable in equation

I have the variables
X = 1x20 vector of doubles
i = 0:M
j = 0:M
And the equation
sum n=1 to length(X) : (X(n)^(i+j))
Is there a way to obtain an MxM matrix (through the indices i,j) while summing out n in each cell? I tried this with symsum but it doesn't allow indexing with n.
Any help is appreciated!
By reshaping X to a vector of size [1 x 1 x 20] and using implicit expansion a 3D [M+1 x M+1 x 20] array is created then by summing along the third dimension the result can be obtained.
X = rand(1,20);
M = 30;
ii = 0:M;
jj = (0:M).';
Y = reshape(X,1,1,[]);
result = sum(Y.^(ii+jj), 3);
However as the expression Y.^(ii+jj) creates a 3D [M+1 x M+1 x 20] array it may need a large amount of memory that leads to decreased performance.
We know that x^(i+j) can be written as x^i * x^j So the expression can be written as:
result = sum(Y.^ii .* Y.^jj,3);
It has the same memory consumption as the previous method. But when we reach an expression that contains sum of products we should think about converting it to very fast matrix multiplication :
Z = X .^ jj; % A [M+1 x 20] matrix is created(implicit expansion)
result = Z * Z.' % multiply Z by its transpose
So the same result is obtained without the complexity of the other solutions.

Store vector values (x,y,z) in a matrix in a for loop. MATLAB

I wanted to create a matrix (A) that is flexible on size depending on how many data are available in a for loop. My code goes like this.
%Calculate x,y,z values
for i = 1:100
Vx = equation that will solve x values;
Vy = equation that will solve y values;
Vz = equation that will solve z values;
A = [Vx, Vy, Vz];
end
I would like matrix A to have a size of nx3, where n is any number of rows arrived by the calculation of x y z. In other words at after the for loop I would like to have matrix A with size n x 3.

surface plot in Matlab

I am trying to graph a surface with a diagonal matrix, the equation I am trying graph is f = x^TDx, x is a 2 by 1 vector and D is a 2 by 2 matrix.
Here is what have so far, but I keep getting error.
x = linspace(-10,10);
y = linspace(-10,10);
[X,Y] = meshgrid(x,y);
D = [1 0; 0 1];
f = #(x,y) [x,y]*D*[x,y].'; % [x,y] is 1 by 2
contour (X,Y,f(X,Y))
Can someone tell me how to get rid of the error? Thanks
Since x and y have the same length, your diagonal matrix D must be a square matrix of size n x n, with n equal to two times the length of your x or y vectors. The reason why you need to multiply the length by two is because the operation [x,y] concatenates the arrays horizontally thus duplicating one of the dimensions.
In this example D is the Identity matrix. See eye for more information.
x = linspace(-10,10); % x is 1x100
y = linspace(-10,10); % y is 1x100
[X,Y] = meshgrid(x,y); % X is 100x100 and Y is 100x100
D = eye(2*numel(x)); % D is 2*100x2*100 = 200x200
f = #(x,y) [x,y]*D*[x,y].'; % [X,Y] is 100x200 and [X,Y].' is 200x100
contour (X,Y,f(X,Y))
If you want D to be a random diagonal matrix, you can accomplish this combining diag with one of the Random Number Generation functions available, like for example randn.
On the previous example, replace D with the following instruction:
D = diag(randn(1,2*numel(x)));
You can also give the coefficients you choose to the diagonal matrix. To do so, you will need to create the vector of coefficients manually, making sure that it has the adequate length, so that it satisfies the conditions explained at the beginning of this post.
Try now replacing D with the following instructions:
v = 1:2*numel(x); % vector of coefficients: v = [1 2 ... 200]
D = diag(v);

Multiplication of vectors in two loops

I want to multiply two vectors to produce a matrix.
I have a vector 1*m and another 1*n which are in my case V (1*71) and I (1*315). The other vectors have same length as I.
I want to multiply every value of I with all values of V and have the answer in a matrix where every row or column of new matrix is I(t).*V
Ir and Temp are vectors with the size of 1*315 and all the variables have the same length and T is 315.
The other parameters that you see in the code are constant values.
This is the code :
function [I] = solar2diodedyn( Ir,time,Temp )
V = 0:0.01:0.7; %open circuit voltage of one cell in V.
for t=1:time;
T(t)= Temp(t)+273;
Vt(t)=(k*T(t))/q;
Iph(t) = Isc_cell*(Ir(t)/1000)*(1+(T_co*(Temp(t)-25)));
I0(t)=Is1*((T(t)/Tmeas)^(3/n1))*exp(Eg*((T(t)/Tmeas)-1)/(n1*Vt(t)));
I02(t)=Is2*((T(t)/Tmeas)^(3/n2))*exp(Eg*((T(t)/Tmeas)-1)/(n2*Vt(t)));
I(t) = zeros(size(t));
i=length(V);
for x=1:i
I(t) = Iph(t) - I0(t)*(exp((V(x)+I(t)*Rs)/(n1*Vt(t)))-1)-I02(t)*(exp((V(x)+I(t)*Rs)/(n2*Vt(t)))-1)-((V(x)+I(t)*Rs)/Rsh);
end
end
Thanks in advance
If you have two vectors x (of size 1-by-n) and y (of size 1-by-m) and you want a matrix M of size n-by-m such that M(i,j) = x(i) * y(j) then you are trying to compute the outer product of x and y.
This can be done easily with matlab
>> M = x.' * y;