Parfor loop with 3D structure matrix

조회 수: 2 (최근 30일)
Reza Ziazi
Reza Ziazi 2018년 12월 2일
편집: Reza Ziazi 2019년 1월 10일
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

답변 (1개)

KSSV
KSSV 2019년 1월 10일
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
Reza Ziazi
Reza Ziazi 2019년 1월 10일
편집: Reza Ziazi 2019년 1월 10일
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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by