Input matrix row values iteratively into a function - matlab

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)

Related

Sub-tensor of a tensor with arbitrary degree in matlab

I have a function where I input a tensor T of arbitrary degree d which is of size n1 x n2 x ... x nd. I want to output the tensor T(1:r,1:r,...,1:r). In other words, an r x r x ... x r sub-tensor with d number of r's. I'm having difficulty working around the variable number 1:r's. Ideally I'd like to do this without reshaping T if possible.
This is quite straightforward to do with cell arrays, consider following example:
% example setup
a = ones(3,3,3,3);
r = 2;
% create indices in cell array
b = cell(1,ndims(a));
b(:) = {1:r};
% evaluate
c = a(b{:});
disp(size(c))

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.

create matrix function of vector input variable (Matlab)

I'm having trouble creating a function that does what I want. I'm trying to create an anonymous function that, on accepting a vector of length N produces an NxN matrix. I'd like to populate each element of the matrix (ie, with a loop). A short example to be more specific:
N = 2;
Qjk = #(x,y) x * y;
for j = 1:N
for k = 1:N
Q(j,k) =#(x) Qjk(x(k),rand);
end
end
In the end this should produce, eg.:
Q = #(x) [.23*x(1), .16*x(2); .95*x(1), .62*x(2)]
I can write the final expression above by hand and it works as required, but I'm unable to automate this process with a loop/vectorization. Thanks.
So it is my understanding that you want to create a N x N matrix of elements where the input is a vector of length N?... and more specifically, you wish to take each element in the input vector x and generate a new 1 x N vector where each element in x gets scaled by this new 1 x N vector?
You can avoid loops by using bsxfun:
Q = bsxfun(#times, x(:).', rand(numel(x)));
I'm not sure what shape x is, whether it's a row or column vector but I'm not going to make any assumptions. x(:).' will ensure that your vector becomes a row vector. However, if you want to get your code working as it, get rid of the anonymous function declaration within the actual loop itself. Also, you'll probably want to call Qjk as that is the function you declared, not Q as that is the matrix you are trying to populate.
Simply do:
N = 2;
Q = zeros(N); % New - Allocate to be more efficient
Qjk = #(x,y) x * y;
for j = 1:N
for k = 1:N
Q(j,k) = Qjk(x(k),rand); % Change
end
end

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;