Matlab: 2D Discrete Fourier Transform and Inverse - matlab

I'm trying to run a program in matlab to obtain the direct and inverse DFT for a grey scale image, but I'm not able to recover the original image after applying the inverse. I'm getting complex numbers as my inverse output. Is like i'm losing information. Any ideas on this? Here is my code:
%2D discrete Fourier transform
%Image Dimension
M=3;
N=3;
f=zeros(M,N);
f(2,1:3)=1;
f(3,1:3)=0.5;
f(1,2)=0.5;
f(3,2)=1;
f(2,2)=0;
figure;imshow(f,[0 1],'InitialMagnification','fit')
%Direct transform
for u=0:1:M-1
for v=0:1:N-1
for x=1:1:M
for y=1:1:N
F(u+1,v+1)=f(x,y)*exp(-2*pi*(1i)*((u*(x-1)/M)+(v*(y-1)/N)));
end
end
end
end
Fab=abs(F);
figure;imshow(Fab,[0 1],'InitialMagnification','fit')
%Inverse Transform
for x=0:1:M-1
for y=0:1:N-1
for u=1:1:M
for v=1:1:N
z(x+1,y+1)=(1/M*N)*F(u,v)*exp(2*pi*(1i)*(((u-1)*x/M)+((v-1)*y/N)));
end
end
end
end
figure;imshow(real(z),[0 1],'InitialMagnification','fit')

There are a couple of issues with your code:
You are not applying the definition of the DFT (or IDFT) correctly: you need to sum over the original variable(s) to obtain the transform. See the formula here; notice the sum.
In the IDFT the normalization constant should be 1/(M*N) (not 1/M*N).
Note also that the code could be made mucho more compact by vectorization, avoiding the loops; or just using the fft2 and ifft2 functions. I assume you want to compute it manually and "low-level" to verify the results.
The code, with the two corrections, is as follows. The modifications are marked with comments.
M=3;
N=3;
f=zeros(M,N);
f(2,1:3)=1;
f(3,1:3)=0.5;
f(1,2)=0.5;
f(3,2)=1;
f(2,2)=0;
figure;imshow(f,[0 1],'InitialMagnification','fit')
%Direct transform
F = zeros(M,N); % initiallize to 0
for u=0:1:M-1
for v=0:1:N-1
for x=1:1:M
for y=1:1:N
F(u+1,v+1) = F(u+1,v+1) + ...
f(x,y)*exp(-2*pi*(1i)*((u*(x-1)/M)+(v*(y-1)/N))); % add term
end
end
end
end
Fab=abs(F);
figure;imshow(Fab,[0 1],'InitialMagnification','fit')
%Inverse Transform
z = zeros(M,N);
for x=0:1:M-1
for y=0:1:N-1
for u=1:1:M
for v=1:1:N
z(x+1,y+1) = z(x+1,y+1) + (1/(M*N)) * ... % corrected scale factor
F(u,v)*exp(2*pi*(1i)*(((u-1)*x/M)+((v-1)*y/N))); % add term
end
end
end
end
figure;imshow(real(z),[0 1],'InitialMagnification','fit')
Now the original and recovered image differ only by very small values, of the order of eps, due to the usual floating-point inaccuacies:
>> f-z
ans =
1.0e-15 *
Columns 1 through 2
0.180411241501588 + 0.666133814775094i -0.111022302462516 - 0.027755575615629i
0.000000000000000 + 0.027755575615629i 0.277555756156289 + 0.212603775716506i
0.000000000000000 - 0.194289029309402i 0.000000000000000 + 0.027755575615629i
Column 3
-0.194289029309402 - 0.027755575615629i
-0.222044604925031 - 0.055511151231258i
0.111022302462516 - 0.111022302462516i

Firstly, the biggest error is that you are computing the Fourier transform incorrectly. When computing F, you need to be summing over x and y, which you are not doing. Here's how to rectify that:
F = zeros(M, N);
for u=0:1:M-1
for v=0:1:N-1
for x=1:1:M
for y=1:1:N
F(u+1,v+1)=F(u+1,v+1) + f(x,y)*exp(-2*pi*(1i)*((u*(x-1)/M)+(v*(y-1)/N)));
end
end
end
end
Secondly, in the inverse transform, your bracketing is incorrect. It should be 1/(M*N) not (1/M*N).
As an aside, at the cost of a bit more memory, you can speed up the computation by not nesting so many loops. Namely, when computing the FFT, do the following instead
x = (1:1:M)'; % x is a column vector
y = (1:1:N) ; % y is a row vector
for u = 0:1:M-1
for v = 0:1:N-1
F2(u+1,v+1) = sum(f .* exp(-2i * pi * (u*(x-1)/M + v*(y-1)/N)), 'all');
end
end
To take this method to the extreme, i.e. not using any loops at all, you would do the following (though this is not recommended, since you would lose code readability and the memory cost would increase exponentially)
x = (1:1:M)'; % x is in dimension 1
y = (1:1:N) ; % y is in dimension 2
u = permute(0:1:M-1, [1, 3, 2]); % x-freqs in dimension 3
v = permute(0:1:N-1, [1, 4, 3, 2]); % y-freqs in dimension 4
% sum the exponential terms in x and y, which are in dimensions 1 and 2.
% If you are using r2018a or older, the below summation should be
% sum(sum(..., 1), 2)
% instead of
% sum(..., [1,2])
F3 = sum(f .* exp(-2i * pi * (u.*(x-1)/M + v.*(y-1)/N)), [1, 2]);
% The resulting array F3 is 1 x 1 x M x N, to make it M x N, simply shiftdim or squeeze
F3 = squeeze(F3);

Related

Matlab Plotting by Conditional Statements

I am attempting to plot the wave equation for a single time step, t, in matlab based on an array of x that are passed into a function, u.
I am not very familiar with matlab and am not sure if this is the proper way to iterate through all x values and plot them. The process does not seem entirely similar to something like python and matplotlib.
EDIT: This code does not seem to be executing properly, how then can I iterate through the array and plot? ex: for element in x: do function
Thanks,
% defining the bounds of my x values
x=-10:.02:10;
% defining my time step, t
t = 1;
x1=[0 0];
y1=[-0.01 0.01];
x2=[-10 10];
y2=[0 0];
% defining some constants to make below equation simpler
xpt2= x + t;
xmt2= x - t;
% plotting based on the values of x - should iterate through the array?
if abs(x) > 1
u = 0.5 .* ((-(xpt2) .* exp(-abs(xpt2))./abs(xpt2)) + ((xmt2).*exp(-abs(xmt2))./abs(xmt2)));
plot(x,u,x1,y1,x2,y2);
xlabel('t=1');ylabel('u');
else
u = 0.5 .* abs(xpt2) + 0.5 .* abs(xmt2) + 0.5 .* (-(xpt2) .* exp(-abs(xpt2)./abs(xpt2)) + ((xmt2).*exp(-abs(xmt2))./abs(xmt2)));
plot(x,u,x1,y1,x2,y2);
xlabel('t=1');ylabel('u');
end
This code may not solve your issue but it may help you to find the error. I expect the error in the else part.
I use for loop to make if-clause work while #slayer way is more professional to work without a loop.
% defining the bounds of my x values
close all
clear
x=-10:.02:10;
% defining my time step, t
t = 1;
x1=[0 0];
y1=[-0.01 0.01];
x2=[-10 10];
y2=[0 0];
% defining some constants to make below equation simpler
xpt2= x + t;
xmt2= x - t;
% plotting based on the values of x - should iterate through the array?
for i=1:length(x)
if abs(x(i)) > 1
u(i) = 0.5 .* ((-(xpt2(i)) .* exp(-abs(xpt2(i)))./abs(xpt2(i))) + ((xmt2(i)).*exp(-abs(xmt2(i)))./abs(xmt2(i))));
else
u(i) = 0.5 .* abs(xpt2(i)) + 0.5 .* abs(xmt2(i)) + 0.5 .* (-(xpt2(i)) .* exp(-abs(xpt2(i))./abs(xpt2(i))) + ((xmt2(i)).*exp(-abs(xmt2(i)))./abs(xmt2(i))));
end
%display step by step
plot(x(1:i),u)
hold on
plot(x1,y1)
plot(x2,y2);
xlabel('t=1');ylabel('u');
pause(1/1000)
end
plot(x,u)
hold on
plot(x1,y1)
plot(x2,y2);
xlabel('t=1');ylabel('u');
You have a number of issues with your code.
1) Your conditional is on a vector so how can you check a conditional for every point in your vector? Well you can't this way.
2) You are taking the abs() of a vector but it looks like you want the negative parts to be accounted for? The abs([-1 0 1]) will return output [1 0 1], which makes your entire vector positive and remove the negative parts.
Now I see why you were asking for a for-loop to check the condition of every x variable in the vector. You can do that with:
for ii=1:numel(x) % This iterates through the vector
x(ii) % this accesses the current index of ii
end
But you still don't need a for loop. Instead use a conditional vector to keep track of the neg and pos points in x like:
idx_neg = x < 0; % boolean of all negative points in x
Then use the idx_neg on the vector you want the equation to be applied to. And the invert of the idx for the positive values like:
u = zeros(1, numel(x)); % initialize empty vector for storage
% for positive x values, use ~idx_neg to find the pos points
u(~idx_neg) = 0.5 .* ((-(xpt2(~idx_neg)) .* exp(-abs(xpt2(~idx_neg)))./abs(xpt2(~idx_neg))) + ((xmt2(~idx_neg)).*exp(-abs(xmt2(~idx_neg)))./abs(xmt2(~idx_neg))));
% now apply to neg points in x:
u(idx_neg) = 0.5 .* abs(xpt2(idx_neg(idx_neg))) + 0.5 .* abs(xmt2(idx_neg)) + 0.5 .* (-(xpt2(idx_neg)) .* exp(-abs(xpt2(idx_neg))./abs(xpt2(idx_neg))) + ((xmt2(idx_neg)).*exp(-abs(xmt2(idx_neg)))./abs(xmt2(idx_neg))));
I didn't check for syntax errors but this is basically what you are looking for.

MATLAB: What's wrong with my quatrotate script?

My version of MATLAB doesn't have the quatrotate function included, so I wrote my own using the equation MathWorks provide here. Trouble is, I don't get the same answers they get in their example in my function, or when I hand calculate it.
Under their example if I input the following I should get an n vector [-1 1 1]:
q = [1 0 1 0]; r = [1 1 1]; n = quatrotate(q, r)
n =
-1.0000 1.0000 1.0000
In my function, and by hand, I get:
[-3 1 1]
What am I missing here? The more I search the more confused I get. As far as I can tell the answer should be [-3 1 1].
Here is the function I wrote:
function [n] = quatrotate(q,r)
%Rotate a given acceleration vector by a given quaternion
%
%Inputs:
% q: A matrix containing a set of quaternion vectors of the
% form q = [w,x,y,z]
% r: A matrix containing a set of linear acceleration vectors
% of the form r= [i,j,k] (also known as [x,y,z])
%
% Outputs:
% n: The solved matrix containing the rotated vector of each linear
% acceleration component
%
%This assumes that the quaternion is normalised (sqw + sqx + sqy + sqz =1),
%if not it should be normalised before doing the conversion.
%To normalise divide qx, qy, qz and qw by n where n=sqrt(qx2 + qy2 + qz2 + qw2)
for k = 1:size(q,1)
rot=[(1-2.*q(k,3).^2-2.*q(k,4).^2) 2.*(q(k,2).*q(k,3)+q(k,1).*q(k,4))...
2.*(q(k,2).*q(k,4)-q(k,1).*q(k,3));2.*(q(k,2).*q(k,3)-q(k,1).*q(k,4))...
(1-2.*q(k,2).^2-2.*q(k,4).^2) 2.*(q(k,3).*q(k,4)+q(k,1).*q(k,2));...
2.*(q(k,2).*q(k,4)+q(k,1).*q(k,3)) 2.*(q(k,3).*q(k,4)-q(k,1).*q(k,2))...
(1-2.*q(k,2).^2-2.*q(k,3).^2)];
n(k,:) = rot*r(k,:)';
end
Thanks in advance!
first of all you need to calculate the modulus of the given Quaternion q:
for index = size(q,1):-1:1
mod(index,:) = norm(q(index,:),2);
end
Then normalize it:
qn = q./(mod* ones(1,4));
Now calculate the Direct Cosine Matrix using these formulae:
dcm = zeros(3,3,size(qn,1));
dcm(1,1,:) = qn(:,1).^2 + qn(:,2).^2 - qn(:,3).^2 - qn(:,4).^2;
dcm(1,2,:) = 2.*(qn(:,2).*qn(:,3) + qn(:,1).*qn(:,4));
dcm(1,3,:) = 2.*(qn(:,2).*qn(:,4) - qn(:,1).*qn(:,3));
dcm(2,1,:) = 2.*(qn(:,2).*qn(:,3) - qn(:,1).*qn(:,4));
dcm(2,2,:) = qn(:,1).^2 - qn(:,2).^2 + qn(:,3).^2 - qn(:,4).^2;
dcm(2,3,:) = 2.*(qn(:,3).*qn(:,4) + qn(:,1).*qn(:,2));
dcm(3,1,:) = 2.*(qn(:,2).*qn(:,4) + qn(:,1).*qn(:,3));
dcm(3,2,:) = 2.*(qn(:,3).*qn(:,4) - qn(:,1).*qn(:,2));
dcm(3,3,:) = qn(:,1).^2 - qn(:,2).^2 - qn(:,3).^2 + qn(:,4).^2;
According to MATLAB documents, the rotation of a vector r by the calculated dcm can be found as follows:
if ( size(q,1) == 1 )
% Q is 1-by-4
qout = (dcm*r')';
elseif (size(r,1) == 1)
% R is 1-by-3
for i = size(q,1):-1:1
qout(i,:) = (dcm(:,:,i)*r')';
end
else
% Q is M-by-4 and R is M-by-3
for i = size(q,1):-1:1
qout(i,:) = (dcm(:,:,i)*r(i,:)')';
end
end
Well first of all, in order for quatrotate to work, you need to use a unit quaternion (i.e. length of 1).
Second of all, I see that you are using the matrix provided by the MATLAB page. I've recently derived that matrix myself and found that the MATLAB page has the wrong matrix.
According to this (page 45), rotating a vector by a quaternion is
p' = p + 2w(v × p)+2(v × (v × p))
Where,
p' is the output vector after rotation,
p is the starting vector to be rotated,
w is the first coefficient in the quaternion,
v is the vector of [second coefficent, third coefficient, fourth coefficient] of the quaternion,
× is the cross product operand.
I encourage you to go to the link and derive the matrix yourself. You will see that the matrix provided on the MATLAB page has the wrong additions and subtractions.
This is what's said on the MATLAB page:
Here's my derivation (here the quaternion is [q0, q1, q2, q3] and the vector is [x, y, z]):
First row:
Second row:
Third row:
Here you can see that the signs are incorrect on MATLAB's website. I've emailed them about the error and is waiting to hear back.

simplifying the formula for EM

I want to compute below formula in Matlab (E-step of EM for Multinomial Mixture Model),
g and θ are matrix , θ and λ have below constrains:
but count of m is more than 1593 and when compute product of θ, number get very small and Matlab save it with zero.
Anyone can simplifying the g formula or use other tricks to solve this problem?
update:
data:
data.txt
(after downloads, change file extension to 'mat')
code:
function EM(data)
%% initialize
K=2;
[N M]=size(data);
g=zeros(N,K);
landa=ones(K,1) .* 0.5;
theta = rand(M, K);
theta = bsxfun(#rdivide, theta, sum(theta,1))';
%% EM
for i=1:10
%% E Step
for n=1:N
normalize=0;
for k=1:K
g(n,k)=landa(k) * prod(theta(k,:) .^ data(n,:));
normalize=normalize + landa(k) * prod(theta(k,:) .^ data(n,:));
end
g(n,:)=g(n,:) ./ normalize;
end
%% M Step
for k=1:K
landa(k)=sum(g(:,k)) / N ;
for m=1:M
theta(k,m)=(sum(g(:,k) .* data(:,m)) + 1) / (sum(g(:,k) .* sum(data,2)) + M);
end
end
end
end
You can use computations on logarithms instead of the actual values to avoid underflow problems.
To start things, we slightly reformat the E step code:
for n = 1 : N
for k = 1 : K
g(n, k) = lambda(k) * prod(theta(k, :) .^ data(n, :));
end
end
g = bsxfun(#rdivide, g, sum(g, 2));
So instead of accumulating the denominator in an extra variable normalize, we do the normalization in one step after both loops.
Now we introduce a variable lg with contains the logarithm of g:
for n = 1 : N
for k = 1 : K
lg(n, k) = log(lambda(k)) + sum(log(theta(k, :)) .* data(n, :));
end
end
g = exp(lg);
g = bsxfun(#rdivide, g, sum(g, 2));
So far, nothing is achieved. The underflow is just moved from within the loop to the conversion from lg to g via the exponential afterwards.
But, in the next line there is the normalization step, which means that the correct value of g is not really necessary: All that is important is that different values have the correct ratios between them. This means we can divide all values that jointly enter a normalization by an arbitrary constant, without changing the end result. On the logarithmic scale, this means subtracting something, and we choose this something to be the arithmetic mean of lg (corresponding to the harmonic mean of g):
lg = bsxfun(#minus, lg, mean(lg, 2));
g = exp(lg);
g = bsxfun(#rdivide, g, sum(g, 2));
Via the subtraction, logarithmic values are moved from something like -2000, which doesn't survive the exponential, to something like +50 or -30. Values of g now are sensible, and can be easily normalized to reach the correct end result.

Using MATLAB to write a function that implements Newton's method in two dimensions

I am trying to write a function that implements Newton's method in two dimensions and whilst I have done this, I have to now adjust my script so that the input parameters of my function must be f(x) in a column vector, the Jacobian matrix of f(x), the initial guess x0 and the tolerance where the function f(x) and its Jacobian matrix are in separate .m files.
As an example of a script I wrote that implements Newton's method, I have:
n=0; %initialize iteration counter
eps=1; %initialize error
x=[1;1]; %set starting value
%Computation loop
while eps>1e-10&n<100
g=[x(1)^2+x(2)^3-1;x(1)^4-x(2)^4+x(1)*x(2)]; %g(x)
eps=abs(g(1))+abs(g(2)); %error
Jg=[2*x(1),3*x(2)^2;4*x(1)^3+x(2),-4*x(2)^3+x(1)]; %Jacobian
y=x-Jg\g; %iterate
x=y; %update x
n=n+1; %counter+1
end
n,x,eps %display end values
So with this script, I had implemented the function and the Jacobian matrix into the actual script and I am struggling to work out how I can actually create a script with the input parameters required.
Thanks!
If you don't mind, I'd like to restructure your code so that it is more dynamic and more user friendly to read.
Let's start with some preliminaries. If you want to make your script truly dynamic, then I would recommend that you use the Symbolic Math Toolbox. This way, you can use MATLAB to tackle derivatives of functions for you. You first need to use the syms command, followed by any variable you want. This tells MATLAB that you are now going to treat this variable as "symbolic" (i.e. not a constant). Let's start with some basics:
syms x;
y = 2*x^2 + 6*x + 3;
dy = diff(y); % Derivative with respect to x. Should give 4*x + 6;
out = subs(y, 3); % The subs command will substitute all x's in y with the value 3
% This should give 2*(3^2) + 6*3 + 3 = 39
Because this is 2D, we're going to need 2D functions... so let's define x and y as variables. The way you call the subs command will be slightly different:
syms x, y; % Two variables now
z = 2*x*y^2 + 6*y + x;
dzx = diff(z, 'x'); % Differentiate with respect to x - Should give 2*y^2 + 1
dzy = diff(z, 'y'); % Differentiate with respect to y - Should give 4*x*y + 6
out = subs(z, {x, y}, [2, 3]); % For z, with variables x,y, substitute x = 2, y = 3
% Should give 56
One more thing... we can place equations into vectors or matrices and use subs to simultaneously substitute all values of x and y into each equation.
syms x, y;
z1 = 3*x + 6*y + 3;
z2 = 3*y + 4*y + 4;
f = [z1; z2];
out = subs(f, {x,y}, [2, 3]); % Produces a 2 x 1 vector with [27; 25]
We can do the same thing for matrices, but for brevity I won't show you how to do that. I will defer to the code and you can see it then.
Now that we have that established, let's tackle your code one piece at a time to truly make this dynamic. Your function requires the initial guess x0, the function f(x) as a column vector, the Jacobian matrix as a 2 x 2 matrix and the tolerance tol.
Before you run your script, you will need to generate your parameters:
syms x y; % Make x,y symbolic
f1 = x^2 + y^3 - 1; % Make your two equations (from your example)
f2 = x^4 - y^4 + x*y;
f = [f1; f2]; % f(x) vector
% Jacobian matrix
J = [diff(f1, 'x') diff(f1, 'y'); diff(f2, 'x') diff(f2, 'y')];
% Initial vector
x0 = [1; 1];
% Tolerance:
tol = 1e-10;
Now, make your script into a function:
% To run in MATLAB, do:
% [n, xout, tol] = Jacobian2D(f, J, x0, tol);
% disp('n = '); disp(n); disp('x = '); disp(xout); disp('tol = '); disp(tol);
function [n, xout, tol] = Jacobian2D(f, J, x0, tol)
% Just to be sure...
syms x, y;
% Initialize error
ep = 1; % Note: eps is a reserved keyword in MATLAB
% Initialize counter
n = 0;
% For the beginning of the loop
% Must transpose into a row vector as this is required by subs
xout = x0';
% Computation loop
while ep > tol && n < 100
g = subs(f, {x,y}, xout); %g(x)
ep = abs(g(1)) + abs(g(2)); %error
Jg = subs(J, {x,y}, xout); %Jacobian
yout = xout - Jg\g; %iterate
xout = yout; %update x
n = n + 1; %counter+1
end
% Transpose and convert back to number representation
xout = double(xout');
I should probably tell you that when you're doing computation using the Symbolic Math Toolbox, the data type of the numbers as you're calculating them are a sym object. You probably want to convert these back into real numbers and so you can use double to cast them back. However, if you leave them in the sym format, it displays your numbers as neat fractions if that's what you're looking for. Cast to double if you want the decimal point representation.
Now when you run this function, it should give you what you're looking for. I have not tested this code, but I'm pretty sure this will work.
Happy to answer any more questions you may have. Hope this helps.
Cheers!

Auto-covariance and Cross Covariance Function in Matlab without using imbuilt functions

x and y are 1x100000 vectors.
I have calculated the mean and variance of x and y. When I want to calculate the autocovariance and cross covariance function the simulation lasts maybe 5 minutes because of my loops. It is not allowed to use xcorr, xcov, mean, cov, var etc.
Please help me.
Thanks in advance.
%%Mean of Vector x
Nx=length(x);
mx= sum(x)/Nx;
%%Mean of Vector y
Ny=length(y);
my=sum(y)/Ny;
%%Variance of x
varx=0;
for i=1:Nx
varx=varx+(abs(x(i)-mx)^(2));
end
varx=varx/Nx;
%%Variance of y
vary=0;
for j=1:Ny
vary=vary+(abs(y(j)-my)^(2));
end
vary=vary/Ny;
%%Auto-Covariance function of x
for k=1:Nx
Cxx(k)=0;
for i=1:(Nx-k+1)
Cxx(k)=Cxx(k)+(x(i+k-1)-mx)*conj((x(i)-my));
end
end
%%Auto-Covariance function of y
for s=1:Ny
Cyy(s)=0;
for j=1:(Ny-s+1)
Cyy(s)=Cyy(s)+(y(j+s-1)-my)*conj((y(j)-mx));
end
end
Use the fact that FFT(corr(x, y)) = FFT(x) * conj(FFTy)):
corrxy = ifft(fft(x) .* conj(fft(y)));
corrxy = [corrxy(end - length(x) + 2:end); corrxy(1:length(x))];
To get the cross-covariance just multiply the correlation by the standard deviations:
covarxy = corrxy * sqrt(varx) * sqrt(vary);
To get the autocovariance, compute the cross covariance between x and itself.
Doing a re-write of this code:
%%Auto-Covariance function of x
for k=1:Nx
Cxx(k)=0;
for i=1:(Nx-k+1)
Cxx(k)=Cxx(k)+(x(i+k-1)-mx)*conj((x(i)-my));
end
end
The following code takes out the inner for-loop:
% x is a [Nx x 1] vector (lets say Nx = 50)
Cxx = zeros(Nx,1); % [Nx x 1] vector of zeros
for k = 1:Nx,
a = (x(k:Nx) -mx); % If k=3, then x(3:50) and a is [Nx-k+1 x 1]
b = (x(1:Nx-k+1)-my); % If k=3, then x(1:48) and b is [Nx-k+1 x 1]
Cxx(k) = a'*conj(b); % Cxx(k) is always 1x1. (*) is a matrix multiply
end
Since x is a really large vector, and the way to take out the the last for-loop for k=1:Nx is to make a [Nx x Nx] matrix, I'm going to leave it at the above answer for now. Plus, if you have the parfor function in the Parallel Computing Toolbox then you can parallelize it to make it run even faster.