Sub-tensor of a tensor with arbitrary degree in matlab - 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))

Related

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.

Input matrix row values iteratively into a function

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)

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

Matlab: work with 2NxN matrix

I have a matrix 2NxN.
And I want get some parametrs by this matrix. For example it:
How, I can do it?
You may want to break your 12x6 matrix, into two 6x6 matrix; let's say: Z and Zb (last one for z bar). Odd rows are Z and evens are Zb.
Considering M to be the combined matrices:
Z = M(1:2:end,:)
Zb = M(2:2:end,:)
read about the colon(:) operator and end to see what 1:2:end means.
Hope it helps.
From what I understand here are the first three:
% Random Matrix
% Needs to be defined before the functions since the functions look for
% the m variable
m = rand(12,6);
% Function 1
p = #(i,j) sign(m(i,j)+m(i+1,j)) * max(abs(m(i,j)),abs(m(i+1,j)));
p(2,2)
% Function 2 - Avg of row
pavg = #(i) mean(m(i,:));
pavg(2)
% Function 3
c = #(i,j) abs(m(i,j)+m(i+1,j)) / (abs(m(i,j)) + abs(m(i+1,j)));
c(2,2)

Using matlabs regress like polyfit

I have:
x = [1970:1:2000]
y = [data]
size(x) = [30,1]
size(y) = [30,1]
I want:
% Yl = kx + m, where
[k,m] = polyfit(x,y,1)
For some reason i have to use "regress" for this.
Using k = regress(x,y) gives some totally random value that i have no idea where it comes from. How do it?
The number of outputs you get in "k" is dependant on the size of input X, so you will not get both m and k just by putting in your x and y straight. From the docs:
b = regress(y,X) returns a p-by-1 vector b of coefficient estimates for a multilinear regression of the responses in y on the predictors in X. X is an n-by-p matrix of p predictors at each of n observations. y is an n-by-1 vector of observed responses.
It is not exactly stated, but the example in the help docs using the carsmall inbuilt dataset shows you how to set this up. For your case, you'd want:
X = [ones(size(x)) x]; % make sure this is 30 x 2
b = regress(y,X); % y should be 30 x 1, b should be 2 x 1
b(1) should then be your m, and b(2) your k.
regress can also provide additional outputs, such as confidence intervals, residuals, statistics such as r-squared, etc. The input remains the same, you'd just change the outputs:
[b,bint,r,rint,stats] = regress(y,X);