在 matlab 中执行 parforlop

execution of parforlop in matlab

我有模拟我实际代码的示例代码。我在 parfor 循环之外有单元格数组的地方。我必须对字符串执行计算,数字输出将存储在数组中,我可以在每个 parfor 循环后将其写入 csv 文件。所以我做了伪代码。但我无法让它执行。错误信息是:"subscription mismatch at line 6"。

ftemp=fopen('temp.csv','w');
    march=cell(1,20);tc=0;
    march={'ab' 'cd' 'ef' 'gh' 'ij' 'kl' 'mn' 'op' 'qr' 'st' 'uv' 'AB' 'CD' 'EF' 'GH' 'IJ' 'KL' 'MN' 'OP' 'QR'};
    for i=1:10
        matlabpool open 4;
        parfor j=1:1:20
        a(j,1)=randi(200,1,1);
        b(j,2)=j+tc;
        c(j,3)=march{1,j};
        d(j,4)=(randi(200,1,1)/200);
        end
        fprintf(ftemp,'%d\t%d\t%s\t%f',a,b,c,d);
        matlabpool close
        clear a b c d;
        tc=tc+20;
    end
    fclose(ftemp);
    quit

错误的原因是您试图在第 9 行将单元格分配到数组中。 我对您的代码做了一些更改,它们在评论中有描述。

ftemp=fopen('temp.csv','w');
march=cell(1,20);tc=0;
march={'ab' 'cd' 'ef' 'gh' 'ij' 'kl' 'mn' 'op' 'qr' 'st' 'uv' 'AB' 'CD' 'EF' 'GH' 'IJ' 'KL' 'MN' 'OP' 'QR'};
for i=1:10
    parpool local; % here in my version of Matlab 2015 there are no more "matlabpool" if it doesen't work change it back
    parfor j=1:20
        %i changed variable a(j,1) into a(j) and b(j,2) into b(j): may
        %contain empty arrays , idem: for c and d
        a(j)=randi(200,1,1);
        b(j)=j+tc;
        c{j}=march{1,j};  % changed c(j)= march{1,j}; : cause of error 
        d(j)=randi(200,1,1)/200;
    end
    fprintf(ftemp,'%d\t%d\t%c\t%d',a,b,char(c),d); % char(c) in order to convert cell array to array of strings
    delete(gcp)  % there isn't such thing  "matlabpool close" , the right expression is "delete(gcp)"
    clear a b c d;
    tc=tc+20;
end
fclose(ftemp);
%quit : i remove because it closes matlab , please put it back if u really
%want to close matlab after operation

注意:如果 parpool 对您的 matlab 版本不起作用,请将其改回您使用的表达式 matlabpool open 4;

ftemp=fopen('temp.csv','w');
march=cell(1,20);tc=0;
march={'ab';'cd';'ef';'gh';'ij';'klm';'mn';'op';'qr';'st';'uv';'ABls';'CD';'E3F';'GH';'IJ';'dynaKL';'MN';'OP';'QR'};
matlabpool open 4;
for k=1:10
    %parpool local; % here in my version of Matlab 2015 there are no more "matlabpool" if it doesen't work change it back
    parfor j=1:20
        %i changed variable a(j,1) into a(j) and b(j,2) into b(j): may
        %contain empty arrays , idem: for c and d
        a(j)=randi(200,1,1);
        b(j)=j+tc;% indexing purposes to identify the order when parallel processing going on
        if length(march{j})>2 % this kind of conditions and computations are there in my actual code
            c{j}='skip';
        else
        c{j}=march{j};
        end
        %d(j)=randi(200,1,1)/200;
    end
    fprintf(ftemp,'%d\t%d\t%s\n',a,b,c{:}); % char(c) in order to convert cell array to array of strings
     clear a b c;
    tc=tc+20;
end
fclose(ftemp);
matlabpool close;

但是我无法得到程序的输出

130 127 Abf5ȱ3³]À@¾
1   2     


97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  168 57  ®$  «E�,vp|}¤
21  22   !"#$%&'(
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  88  125 S([g 0'0Â"
41  42  +,-./0123456789:;<
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  161 170 7Na�U;6´q¶nl®v¤
61  62  ?@ABCDEFGHIJKLMNOP
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  171 139 ZF­ºPµ~R(`b[
81  82  STUVWXYZ[\]^_`abcd
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  132 80  ||£)Á1[ªavb-
101 102 ghijklmnopqrstuvwx
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  186 63  ª`neepw»fq°r1
121 122 {|}~�
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  51  160 M(1´uOAF¦{¬©
141 142 ��� 
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  90  21  <,uZgh�lu¨B,¦
0
161 162 £¤¥¦§¨©ª«¬­®¯°±²³´
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  192 160 o£¿^hyµÄ¨�Ä�O
181 182 ·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈ
97  98  cd
101 102 gh
105 106 skip
109 110 op
113 114 st
117 118 skip
67  68  skip
71  72  IJ
115 107 ip
77  78  OP
81  82  

我的意思是说为什么只进行了 124 次迭代而不是 200 次迭代。以及为什么这些任意输出出现在第 3 列中

对于意外的输出和输出中的行数,这是因为 fprintf 的错误使用。我们需要逐个打印元素,这意味着 fprintf 必须在 parfor 循环内,因此您的代码应如下所示:

ftemp=fopen('temp.csv','w');
march=cell(1,20);tc=0;
march={'ab';'cd';'ef';'gh';'ij';'klm';'mn';'op';'qr';'st';'uv';'ABls';'CD';'E3F';'GH';'IJ';'dynaKL';'MN';'OP';'QR'};
matlabpool open 4;
for k=1:10
    %parpool local; % here in my version of Matlab 2015 there are no more "matlabpool" if it doesen't work change it back
    parfor j=1:20
        %i changed variable a(j,1) into a(j) and b(j,2) into b(j): may
        %contain empty arrays , idem: for c and d
        a(j)=randi(200,1,1);
        b(j)=j+tc;% indexing purposes to identify the order when parallel processing going on
        if length(march{j})>2 % this kind of conditions and computations are there in my actual code
            c{j}='skip';
        else
            c{j}=march{j};
        end
        %d(j)=randi(200,1,1)/200;
        fprintf(ftemp,'%d\t%d\t%s\t\n',a(j),b(j),c{j}); % char(c) in order to convert cell array to array of strings
    end
    clear a b c;
    tc=tc+20;

end
fclose(ftemp);
matlabpool close;

输出是:

9   1   ab
171 2   cd
7   3   ef
98  4   gh
102 5   ij
20  6   skip
53  7   mn
174 8   op
36  9   qr
30  10  st
130 11  uv
127 12  skip
133 13  CD
65  14  skip
118 15  GH
130 16  IJ
139 17  skip
168 18  MN
57  19  OP
25  20  QR
22  21  ab
39  22  cd
83  23  ef
22  24  gh
159 25  ij
40  26  skip
164 27  mn
78  28  op
194 29  qr
88  30  st
125 31  uv
1   32  skip
2   33  CD
112 34  skip
161 35  GH
170 36  IJ
55  37  skip
59  38  MN
18  39  OP
134 40  QR
80  41  ab
118 42  cd
108 43  ef
174 44  gh
97  45  ij
157 46  skip
85  47  mn
98  48  op
40  49  qr
11  50  st
171 51  uv
139 52  skip
90  53  CD
70  54  skip
173 55  GH
150 56  IJ
186 57  skip
155 58  MN
136 59  OP
96  60  QR
158 61  ab
118 62  cd
124 63  ef
127 64  gh
26  65  ij
124 66  skip
91  67  mn
186 68  op
63  69  qr
137 70  st
170 71  uv
98  72  skip
132 73  CD
80  74  skip
160 75  GH
20  76  IJ
156 77  skip
142 78  MN
110 79  OP
51  80  QR
18  81  ab
29  82  cd
40  83  ef
49  84  gh
102 85  ij
113 86  skip
96  87  mn
44  88  op
166 89  qr
90  90  st
21  91  uv
60  92  skip
44  93  CD
166 94  skip
103 95  GH
123 96  IJ
77  97  skip
163 98  MN
138 99  OP
111 100 QR
94  101 ab
133 102 cd
158 103 ef
13  104 gh
26  105 ij
117 106 skip
90  107 mn
58  108 op
156 109 qr
79  110 st
196 111 uv
168 112 skip
192 113 CD
160 114 skip
56  115 GH
129 116 IJ
191 117 skip
157 118 MN
170 119 OP
30  120 QR
137 121 ab
128 122 cd
12  123 ef
38  124 gh
38  125 ij
122 126 skip
97  127 mn
129 128 op
142 129 qr
154 130 st
99  131 uv
85  132 skip
129 133 CD
111 134 skip
108 135 GH
78  136 IJ
102 137 skip
48  138 MN
100 139 OP
89  140 QR
114 141 ab
12  142 cd
184 143 ef
145 144 gh
5   145 ij
48  146 skip
92  147 mn
64  148 op
87  149 qr
32  150 st
136 151 uv
103 152 skip
90  153 CD
73  154 skip
28  155 GH
191 156 IJ
63  157 skip
120 158 MN
104 159 OP
178 160 QR
148 161 ab
36  162 cd
129 163 ef
11  164 gh
172 165 ij
186 166 skip
145 167 mn
142 168 op
150 169 qr
185 170 st
14  171 uv
141 172 skip
22  173 CD
163 174 skip
48  175 GH
164 176 IJ
117 177 skip
25  178 MN
110 179 OP
111 180 QR
175 181 ab
60  182 cd
195 183 ef
44  184 gh
163 185 ij
4   186 skip
103 187 mn
95  188 op
127 189 qr
10  190 st
11  191 uv
182 192 skip
162 193 CD
179 194 skip
76  195 GH
104 196 IJ
153 197 skip
103 198 MN
4   199 OP
154 200 QR

输出也有 200 行。