Parfor loop with 3D structure matrix

1 view (last 30 days)
Reza Ziazi
Reza Ziazi on 2 Dec 2018
Edited: Reza Ziazi on 10 Jan 2019
Hello
I have the following 3 dimensional vector of velocity field with a dimension of 4697×1×1600 which I am trying to put it into a matrix of 77×77×1600. There is no problem with this code but the loop takes three minutes to run and it is a long time for running only one simple part of my code. I have problem using parfor with this current code since it is a structure data and it seems like parfor does not accept structures. I thought maybe the problem is because of indexing or maybe the three dimensional matrix?
Somebody could help me to begin somewhere?
I really appreciate any help in advance.
clear
clc
% Generating the random velocity vector field
vel.V_pix1D.Data1 = rand(4697,1,1600);
vel.V_pix1D.Data2 = rand(4697,1,1600);
vel.V_pix1D.Data3 = rand(4697,1,1600);
vel.V_pix1D.Data4 = rand(4697,1,1600);
vel.U_pix1D.Data1 = rand(4697,1,1600);
vel.U_pix1D.Data2 = rand(4697,1,1600);
vel.U_pix1D.Data3 = rand(4697,1,1600);
vel.U_pix1D.Data4 = rand(4697,1,1600);
% Generating the spatial vector field
x.x_pix1D.Data1 = rand(4697,1,1600);
x.x_pix1D.Data2 = rand(4697,1,1600);
x.x_pix1D.Data3 = rand(4697,1,1600);
x.x_pix1D.Data4 = rand(4697,1,1600);
y.y_pix1D.Data1 = rand(4697,1,1600);
y.y_pix1D.Data2 = rand(4697,1,1600);
y.y_pix1D.Data3 = rand(4697,1,1600);
y.y_pix1D.Data4 = rand(4697,1,1600);
% Preallocation
vel.U_pix2D.Data1 = zeros(77,77,1600);
vel.V_pix2D.Data1 = zeros(77,77,1600);
vel.U_pix2D.Data2 = zeros(77,77,1600);
vel.V_pix2D.Data2 = zeros(77,77,1600);
vel.U_pix2D.Data3 = zeros(77,77,1600);
vel.V_pix2D.Data3 = zeros(77,77,1600);
vel.U_pix2D.Data4 = zeros(77,77,1600);
vel.V_pix2D.Data4 = zeros(77,77,1600);
x.x_pix2D.Data1 = zeros(77,77,1600);
x.x_pix2D.Data2 = zeros(77,77,1600);
x.x_pix2D.Data3 = zeros(77,77,1600);
x.x_pix2D.Data4 = zeros(77,77,1600);
y.y_pix2D.Data1 = zeros(77,77,1600);
y.y_pix2D.Data2 = zeros(77,77,1600);
y.y_pix2D.Data3 = zeros(77,77,1600);
y.y_pix2D.Data4 = zeros(77,77,1600);
n = 0;
imax = 77;
jmax = 61; %61
kmax = 1600;
tic
for k = 1:kmax
for j = 1:jmax
for i = 1:imax
n = n+1;
vel.U_pix2D.Data1(j,i,k) = vel.U_pix1D.Data1(n,1,k); vel.V_pix2D.Data1(j,i,k) = vel.V_pix1D.Data1(n,1,k);
vel.U_pix2D.Data2(j,i,k) = vel.U_pix1D.Data2(n,1,k); vel.V_pix2D.Data2(j,i,k) = vel.V_pix1D.Data2(n,1,k);
vel.U_pix2D.Data3(j,i,k) = vel.U_pix1D.Data3(n,1,k); vel.V_pix2D.Data3(j,i,k) = vel.V_pix1D.Data3(n,1,k);
vel.U_pix2D.Data4(j,i,k) = vel.U_pix1D.Data4(n,1,k); vel.V_pix2D.Data4(j,i,k) = vel.V_pix1D.Data4(n,1,k);
x.x_pix2D.Data1(j,i,k) = x.x_pix1D.Data1(n,1,k);y.y_pix2D.Data1(j,i,k) = y.y_pix1D.Data1(n,1,k);
x.x_pix2D.Data2(j,i,k) = x.x_pix1D.Data2(n,1,k);y.y_pix2D.Data2(j,i,k) = y.y_pix1D.Data2(n,1,k);
x.x_pix2D.Data3(j,i,k) = x.x_pix1D.Data3(n,1,k);y.y_pix2D.Data3(j,i,k) = y.y_pix1D.Data3(n,1,k);
x.x_pix2D.Data4(j,i,k) = x.x_pix1D.Data4(n,1,k);y.y_pix2D.Data4(j,i,k) = y.y_pix1D.Data4(n,1,k);
end
end
n = 0;
end
toc

Answers (1)

KSSV
KSSV on 10 Jan 2019
No loops and parfor required...have a look on reshape.
A = rand(4697,1,1600) ;
nx = 77 ; ny = 61 ;
nt = 1600 ;
B = reshape(A,nx,ny,nt) ;
C = zeros(77,77,nt) ;
C(1:nx,1:ny,:) = B ;
  1 Comment
Reza Ziazi
Reza Ziazi on 10 Jan 2019
Edited: Reza Ziazi on 10 Jan 2019
Thanks for your answer. Yes I totally missed the reshape command. Thanks
This was an example to actually try using a 3-dimensional matrices in structure form. I need to use many loops in my code for analyzing the data so if there is a way to use structures in parallel processing loops would be really helpful.
I looked at many different examples on MATLAB Answers or Parallel Processing Toolbox but there was not any example on how to use structures in parfor loops. Is there any way to do that?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!