I want to compute an outer product of the same vector in Matlab. A representative example would be:
x=rand(1e5,1);
sigma=x*x'-spdiags(x,0,length(x),length(x));
Is there any obvious way to speed this up? x*x' is a symmetric matrix, but have not figured out a way to help Matlab use that information to speed things up.
EDIT: There is a way to do this with loops but I cannot see the benefit yet:
for k=1:length(x)
sigma(k:length(x),k)=x(k).*x(k:length(x));
end
The above might work with a sparse array.
Have you considered using pdist with custom distance function
sigmaCompact = pdist( x(:), #(x, Y) x.*Y );
sigma = squareform(sigmaCompact);
up to the special treatment of sigma( k, k );
Related
I am trying to create this function in MATLAB. From a previous question, I have solved for a as agrid2 and f(a) as fx, whose dimensions are 600*1 and 600*2- these two vectors are absolutely correct:
%calculating gini coefficent
m = 600;
for i = 1:m %expanded length of kgrid of ai previously calculated in invarden.m
mu = sum(agrid2.*fx(m,:));
gini = sum(fx(m,1)*(fx(m,2))*abs(fx(m,1)-fx(m,2)))./(2*mu);
end
However, my code is returning all blank answers.
what did I do wrong? Any advice is greatly appreciated!
It is not clear what the matrices agrid2 and fx are, however mu should be computed outside that for loop, since in the formula (1) it is not included in the summation. So, you should first compute mu, and then G.
Furthermore, from your code it seems that inside the function abs() you are considering f instead of a.
Also, I am pretty sure you need a double for loop, since there are two summations in (1).
For the linear regression, I want to generate the matrix for polynomials of n degree.
if n is 1
X=[x(:), ones(length(x),1)]
if n is 2
X=[x(:).^2 x(:) ones(length(x),1)]
...
if n is 5
X=[x(:).^5 x(:).^4 x(:).^3 x(:).^2 x(:) ones(length(x),1)]
I do not know how to code with matlab if I set n=6 and it will automatically generate the wanted X matrix. Hope you can help me.
This can be easily done with bsxfun:
X = bsxfun(#power, x(:), n:-1:0);
Or, in Matlab versions from R1016b onwards, you can use implicit expansion:
X = x(:).^(n:-1:0);
Check out the polyval function. I believe that will do what you’re looking for.
To get increasing the polynomial to increase in degree, you can increase the length of your p argument using a loop.
If you write edit polyfit you can see how MATLAB have implemented the polyfit command, which is similar to what you are trying to do. In there you will find the code
% Construct the Vandermonde matrix V = [x.^n ... x.^2 x ones(size(x))]
V(:,n+1) = ones(length(x),1,class(x));
for j = n:-1:1
V(:,j) = x.*V(:,j+1);
end
Which constructs the matrix you are interested in. The benefit of this method over the bsxfun is that you only calculate x(:).^n and then saves the intermediary results. Instead of treating all powers as seperate problems, e.g. x(:)^(n-1) as a seperate problem to x(:).^n.
I have a cell array myBasis of sparse matricies B_1,...,B_n.
I want to evaluate with Matlab the matrix Q(i,j) = trace (B^T_i * B_j).
Therefore, I wrote the following code:
for i=1:n
for j=1:n
B=myBasis{i};
C=myBasis{j};
Q(i,j)=trace(B'*C);
end
end
Which takes already 68 seconds when n=1226 and B_i has 50 rows, and 50 colums.
Is there any chance to speed this up? Usually I exclude for-loops from my matlab code in a c++ file - but I have no experience how to handle a sparse cell array in C++.
As noted by Inox Q is symmetric and therefore you only need to explicitly compute half the entries.
Computing trace( B.'*C ) is equivalent to B(:).'*C(:):
trace(B.'*C) = sum_i [B.'*C]_ii = sum_i sum_j B_ij * C_ij
which is the sum of element-wise products and therefore equivalent to B(:).'*C(:).
When explicitly computing trace( B.'*C ) you are actually pre-computing all k-by-k entries of B.'*C only to use the diagonal later on. AFAIK, Matlab does not optimize its calculation to save it from computing all the entries.
Here's a way
for ii = 1:n
B = myBasis{ii};
for jj = ii:n
C = myBasis{jj};
t = full( B(:).'*C(:) ); % equivalent to trace(B'*C)!
Q(ii,jj) = t;
Q(jj,ii) = t;
end
end
PS,
It is best not to use i and j as variable names in Matlab.
PPS,
You should notice that ' operator in Matlab is not matrix transpose, but hermitian conjugate, for actual transpose you need to use .'. In most cases complex numbers are not involved and there is no difference between the two operators, but once complex data is introduced, confusing between the two operators makes debugging quite a mess...
Well, a couple of thoughts
1) Basic stuff: A'*B = (B'*A)' and trace(A) = trace(A'). Well, only this trick cut your calculations by almost 50%. Your Q(i,j) matrix is symmetric, and you only need to calculate n(n+1)/2 terms (and not n²)
2) To calculate the trace you don't need to calculate every term of B'*C, just the diagonal. Nevertheless, I don't know if it's easy to create a script in Matlab that is actually faster then just calculating B'*C (MatLab is pretty fast with matrix operations).
But I would definitely implement (1)
I need to pre-compute the histogram intersection kernel matrices for using LIBSVM in MATLAB.
Assume x, y are two vectors. The kernel function is K(x, y) = sum(min(x, y)). In order to be efficient, the best practice in most cases is to vectorize the operations.
What I want to do is like calculate the kernel matrices like calculating the euclidean distance between two matrices, like pdist2(A, B, 'euclidean'). After defining function 'intKernel', I could calculate the intersection kernel by calling pdist2(A, B, intKernel).
I know the function 'pdist2' may be an option. But I have no idea how to write the self-defined distance function. While, I do not know how to code the intersection kernel between vector(1-by-M) and matrix(M-by-N) in one condense expression.
'repmat' may not be feasible, because the matrix is really large, let us say, 20000-by-360000.
Any help would be appreciated.
Regards,
Peiyun
I think pdist2 is a good option, so I help you to define your distance function.
According to the doc, the self-defined distance function must have 2 inputs: first one is a 1-by-N vector; second one is a M-by-N matrix (be careful of the order!).
To avoid the use of repmat which is indeed memory-consumant, you can use bsxfun to apply some basic operations on data with expansion over singleton dimensions. In your case, you can do the following thing:
distance_kernel = #(x,Y) sum(bsxfun(#min,x,Y),2);
Summation is done over the columns to get a column vector as output.
Then just call pdist2 and you are done.
I have a 3D matrix of a movie (say a matrix M of size J*K*L). I want to flip left right each frame. Using fliplr(M) doesn't work as M must be a 2-D matrix. I know I can use a for loop of the following:
for ii=1:size(M,3)
M(:,:,ii)=fliplr( M(:,:,ii) )
end
Is the a "vectorized" way to do it?
More generally speaking, is the a "vectorized" way to do any of Matlab's matrix manipulations (flipud, repmat, etc) in this case?
Alternatively, you can use simple indexing:
>> M = rand(3,4,5);
>> M(:, end:-1:1, :);
This is a lot faster and less resource intensive than flipdim, and I think a lot cleaner too.
However, for some people, this particular usage of the end keyword is confusing, so if you're one of those people, flipdim will do just fine :)
I think you are looking for
M = flipdim(M, 2);
This flips an N dimensional matrix along the dimension you specify as the second parameter. Thus, the flipud could be replaced with
M = flipdim(M, 1);
Not sure where you are going with the repmat question, but I often find I can use bsxfun instead of repmat. Look it up.