Lets say I have a K-matrix of size [5x5] in Matlab whose all elements are ones. Now I have a second P-matrix of size [25x25] whose all elements are zeros. The first M rows and N columns of P is now updated with K's element.
M = 5;
N = 5;
K = ones(M,N);
P = zeros(M*N, M*N);
P(1:M,1:N) = K;
Now, I want to make the matrix P as circulant matrix. That is the submatrix (K) of P will shift circularly to right. What is the best way to do this in Matlab.
Related
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);
I have an N x N matrix, A, and a vector of row indices, v. I want to replace the diagonal elements of A only for the rows in A specified by v without using a for loop.
For example:
N = 10;
A = rand(N,N); %Random N x N matrix
v = [1 4 6 9 10]; %vector of row indices
%What I want to do but without a for loop:
for i = 1:length(v)
A(v(i),v(i)) = 0;
end
%I thought this would work, but it does not:
%A(v,v) = 0;
I feel like there must a one-line method of doing this, but can't seem to figure out what it would be.
Cheers
Use sub2ind:
A(sub2ind(size(A),v,v)) = 0;
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'm new to Matlab and I'm trying to solve a problem that involves creating a d dimensional multiplication table where each edge goes from 1 to n. The problem statement says that inputting d = 0 should return the number 1 and d = 1 should return a column vector with the elements 1 to n.
Ideally, I would just create a matrix of 1 to n along d dimensions and then iterate through for each element setting it equal to the product of the indices, but I don't know how to create the d dimensional matrix.
Can anyone help me with this problem?
You can create the table with repeated use of bsxfun. At each iteration, the vector 1,2,...,n is shifted to a new dimension and multiplied (with singleton expansion) by the previous result.
%// Data
d = 3;
n = 10;
%// Computations
vector = (1:n).'; %// first dimension: column vector
result = 1; %// initialization
for n = 1:d
result = bsxfun(#times, result, vector); %// new dimension
vector = shiftdim(vector,-1); %// shift to the next dimension
end