Are single loops or dense loops more computationlly efficent in matlab? - matlab

Im am currently writing a code to implement a numerical approximation to the 3D steady state heat equation using finite difference matrix methods. This involves discritising the 2nd order PDE into the matrix A and solving Ax=b. where x is temperature at each of the specified grid points. Further information on this type of question can be found here:
http://people.nas.nasa.gov/~pulliam/Classes/New_notes/Matrix_ODE.pdf
To complete this problem, I have represented the 3D matrix A by a 2D array calling the values in the 1D array b using an indexing function of the form:
i+(j-1)*Nx+Nx*Ny*(k-1)
for the (i,j,k)th element of the 3D matrix where Nx, Ny, Nz are the number of points in the x,y,z coordinates. There ends up being a lot of loop computation in order to create the matrix A and b and I was wondering what is the most computationally efficient and less memory exhaustive way to run these loops, i.e. is it better to use something like
for j=1:Ny
for i=2:Nx-1
b(i+(j-1)*Nx)=D4;
end
end
for j=1:Ny
for i=2:Nx-1
b(i+(j-1)*Nx+Nx*Ny*(Nz-1))=D3;
end
end
or should I condense these into a single loop like:
for j=1:Ny
for i=2:Nx-1
b(i+(j-1)*Nx)=D4;
b(i+(j-1)*Nx+Nx*Ny*(Nz-1))=D3;
end
end
I have preallocated both the arrays A and b. Is there a vectorised way to do this also?

Assuming Nx, Ny, Nz, D3 and D4 to be scalars and that you are using pre-allocation for b with zeros, you may try this vectorized approach -
I = 2:Nx-1; %// Vectors to represent i
J = 1:Ny; %// Vectors to represent j
ind1 = bsxfun(#plus,I,[(J-1)*Nx]'); %//' Indices, 1st set of nested loops
ind2 = bsxfun(#plus,I,[(J-1)*Nx+Nx*Ny*(Nz-1)]'); %//' Indices, 2nd set of loops
b(ind1) = D4; %// Assign values for 1st set
b(ind2) = D3; %// Assign values for 2nd set

The second method should be slightly faster since it performs the same number of calculations with fewer increments of the loop variables. You can look into MATLAB's built-in stopwatch commands tic and toc to time your code. http://www.mathworks.com/help/matlab/ref/tic.html
Something more vectorized might be possible but I would need to know more about the format of the arrays that contain D3 and D4. The reshape() function might be able to help.

Related

How would you do this matrix operation in MATLAB?

Given two random variables X and Y, where X=(x1,..,xn) and Y=(y1,...,yn) in a nx2 matrix A, so A=[X Y], i need to perform the next operation:
median((x-median(x))(y-median(y)))
I'm trying to obtain an estimator of the covariance matrix using the median instead the mean, for a nxt matrix where t represents the number of random variables and n the length of the data set.
So far, I made the next code:
for i=1:n
for j=1:n
a1=median(A(:,i));
a2=median(A(:,j));
SMM(i,j)=median(((A(:,i)-a1(ones(t,1),:)).*(A(:,j)-a2(ones(t,1),:))));
end
end
However, theoretically I must obtain a semidefinite (positive or negative) symmetric matrix, however that's not the case with this code.
Am I making any mistake in the code formulation?
Various points:
For each of your columns of A (x, y), the median (a1, a2) doesn't change. You should compute these outside the loops.
The loops go over n, rather than t, which are the variables and the indices to the output matrix.
I would first subtract the median from each column, to avoid repeatedly doing the same computations:
A = A - median(A,1); % be explicit about which dimension to take the median over!
Next, we'd loop over the txt output elements of the covariance matrix, and compute each of the elements:
t = size(A,2);
SMM = zeros(t,t); % always preallocate output arrays before a loop
for j=1:t
for i=1:t
SMM(i,j) = median(A(:,i).*A(:,j));
end
end
The loop can likely be vectorized, but that leads to a large intermediate matrix, which slow down code also. So it might not be worth the effort to vectorize. Only try it if this code is too slow!
It should also be possible to run the inner loop from i=j:t, to skip computing the redundant half of the symmetric matrix, instead copying over the previously computed values.

Matlab 3D matrix multiplication

In Matlab, I have two m-by-n matrices X and Y, with n>m.
I need to define a 3D m-by-m-by-n matrix Z whose components can be computed as
for i=2:m
for j=i+1:m
for k=1:n
Z(i,j,k) = (Y(j-1,k)-Y(i-1,k))*X(j-1,k);
end
end
end
As these nested loops require a long computational time, I have been looking for a way to define the matrix Z using matrices multiplication, but I have not managed so far. Any suggestion?
You can simply remove the inner loop (over k) by writing
Z(i,j,:) = (Y(j-1,:)-Y(i-1,:)).*X(j-1,:);
Note the .* element-wise multiplication. You can then proceed to remove additional loops in a similar manner.
But note that, most likely, your loop is slow because you don't preallocate the output array. Do this before the loop:
Z = zeros(m,m,n);
You can also gain a bit of speed by reversing the loop order, such that the first index is iterated over in the innermost loop, and the last index is iterated over in the outermost loop. This accesses the matrix data in memory order, making your cache more efficient.

How can I make dot product of vector and matrix faster in MATLAB?

My code works however it's fairly slow and i need to run it multiple times so it's very inefficient. i am positive there is a more efficient way of calculating it.
The code is an implementation of this equation:
where k(x,y) is the dot product of the two vectors
xi and yj are the rows i,j of the two matrices A and B, respectively.
I'd like to also note that the number of rows in each matrix is in the thousands.
here is my code
m=size(A,1);
Kxx=0;
for i=1:m
x=A(i,:);
X=A(i+1:end,:);
Kxx=Kxx+2*sum(dot(ones(m-i,1)*x,X,2));
end
Kxx=Kxx/(m*(m-1));
n=size(B,1);
Kyy=0;
for j=1:n
y=B(j,:);
YY=B(j+1:end,:);
Kyy=Kyy+2*sum(dot(ones(n-j,1)*y,YY,2));
end
Kyy=Kyy/(n*(n-1));
Kxy=0;
for i=1:m
x=A(i,:);
for j=1:n
y=B(j,:);
Kxy=Kxy+dot(x,y);
end
end
Kxy=Kxy*2/(m*n);
Dxy=Kxx+Kyy-Kxy;
Your edit makes our jub much easier. Here's what you just have to do for a fully vectorized solution:
C=A*A'; %'
Kxx=sum(sum(C-diag(diag(C))))/m/(m-1);
C=B*B'; %'
Kyy=sum(sum(C-diag(diag(C))))/n/(n-1);
Kxy=2*mean(reshape(A*B.',[],1)); %'
Dxy=Kxx+Kyy-Kxy;
Thanks to #hiandbaii for pointing out that the equivalent of dot for complex vectors involves the conjugate transpose rather than the transpose.
Original, looping version for historical sentimental reasons:
I'm not sure whether the first two loops can be vectorized without huge memory overhead. So while I figure this out, here's a version in which the first two loops are a bit simplified, and the third loop is replaced by a vectorized operation:
%dummy input data
A=rand(5);
B=rand(5);
m=size(A,1);
Kxx=0;
for l=1:m
x=A(l,:);
X=A(l+1:end,:);
Kxx=Kxx+2*sum(X*x.'); %'
end
Kxx=Kxx/(m*(m-1));
n=size(B,1);
Kyy=0;
for l=1:n
y=B(l,:);
YY=B(l+1:end,:);
Kyy=Kyy+2*sum(YY*y.'); %'
end
Kyy=Kyy/(n*(n-1));
Kxy=2*mean(reshape(A*B.',[],1)); %'
Dxy=Kxx+Kyy-Kxy;

How to calculate matrix entries efficently using Matlab

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)

Build a 3D least squares distance matrix without loops?

I am trying to build a three dimensional matrix out of three vectors where I take the least squares distance between each element of each vector as the entries of the matrix.
For example for the 3d matrix d,
d(m,n,o)=(vec1(m)-vec2(n))^2+(vec1(m)-vec3(o))^2+(vec2(n)-vec1(o))^2
I am currently doing this with a triple for loop:
d=zeros(N,M,O);
for o=1:O
for n=1:N
for m=1:M
d(n,m,o)=(((t(n)-r(m))^2)+((t(n)-z(o))^2)+((r(m)-z(o))^2));
end
end
end
My question is whether there is a quicker, cleverer way to do this for instance for a 2d version of this I could use:
%for n=1:N
% for m=1:M
% d(n,m)=(t(n)-r(m))^2;
% end
%end
d=(repmat(t(:),1,M)-repmat(r(:)',N,1)).^2; %this replaces the nested for loops from above Thanks Georg Schmitz
Whoever Georg Schmitz is came up with a way to use repmat to replace the double for loops in the 2d version. I could of course adapt this method and replace my triple for loops with one for loop that repeats the repmat method (o) number of times, But I feel like there should be a way to do this without loops.
Any ideas? Thanks
You can indeed vectorize the calculation:
%# properly reshape the vectors
vec1 = vec1(:); %# n-by-1
vec2 = reshape(vec2,1,[]); %# 1-by-m
vec3 = reshape(vec3,1,1,[]); %# 1-by-1-by-o
%# use bsxfun to efficiently replicate the arrays
d = bsxfun(#plus,bsxfun(#plus,...
bsxfun(#minus,vec1,vec2).^2,...
bsxfun(#minus,vec2,vec3).^2)),...
bsxfun(#minus,vec3,vec1).^2));
You should try pdist or pdist2 depending on your needs. pdist computes inner distances, while pdist2 computes a pairwise distance matrix.