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;
Related
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?
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.
I have to perform matrix updating by M = M + c*a*a' large number of times, where c is a constant and a is a column vector. If the size of matrix is larger than 1000, this simple updating will cost most of the time of my function, typically more than 1 min counted by profile.
Main codes are:
for i = 1:N
_do something..._
for k = 1:n
a(1:k) = M(1:k,1:k)*p(1:k);
M(1:k,1:k) = M(1:k,1:k)+c*a(1:k)*a(1:k)';
M(1:k, k+1) = b(1:k);
M(k+1, 1:k) = b(1:k)';
M(k+1, k+1) = x;
......
end
end
I have preallocated all variables, column vectors p and b are known, and x is another constant.
As I have large number of data to process by this function, does there exist more efficient alternative to this matrix updating?
You can concatenate a vectors to create a matrix A then apply multiplication just one time.
A =[a1 a2 a3];
M = c * A * A.';
consider the example
A = rand(5,5);
M = 0;
c=4;
for n = 1:5
M = M + c * A(:,n) * A(:,n).';
end
and this one
M1 = c * A * A.'
both M and M1 are equal
Have you tried using bsxfun?
In any case, bsxfun is much faster than regular multiplication, but the vectors/matrices have to be of equal length (which they are for you, aren't they?), and it's operating elementwise (i.e. a Nx1 vector bsx-multiplied with itself yields a Nx1 vector, multiplied with the transpose however yields a NxN matrix).
see https://mathworks.com/help/matlab/ref/bsxfun.html
use as
bsxfun(#times, a, a')
I have a matrix which is 100x1 in size. I wish to input each row value of my matrix into a function iteratively. For example, say L1 represents row 1 of my matrix L, L2 row 2, and so on. Say my function which I seek to input each value of L into is denoted Y. Therefore I seek to input L1 into Y to Produce Y1, L2 for Y2 and so on.
I could really do with help on how to implement this in matlab?
accept
Code is as follows:
load('logregdata.mat')
%%Data set X is a series of coordinates in two dimensions and t represents class labels. Data set is for a binary classification problem.
u = rand;
[w1,w2] = meshgrid(-5:0.1:5,-5:0.1:5);
w = zeros(2,1);
w_all = zeros(100,2);
%Probabilistic term of logistic classifier prob_t = 1./(1+exp(-[w1(:) w2(:)]*X'));
L = sum(log(prob_t).*repmat(t',numel(w1),1),2);
L= L + sum (log(1-prob_t).*repmat(1-t',numel(w1),1),2);
u = rand;
y = log(L/u);
Thanks for all your help in advance.
A 100x1 matrix is just a vector! So you can loop through the entire array like this:
for i = 1:100
do something with Y(L1)
end
In your code u is just a scalar, so you can use simple element-wise operations:
y = log(L./u);
which will give you a vector y in the same size of L such that y(k) = log(L(k)/u)
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.