Assigning nested tables inside a parfor-loop
显示 更早的评论
I use parfor for paralelisation, and create an table as LOG which contains a nested table.
Runing the following code will result in the following Error:
Subscripted assignment dimension mismatch for table variable 'Var2'.
VarTypes={'double','table'};
LOG=table('Size',[1 2],'VariableTypes',VarTypes);
parfor i=1:1
nested_table=table(2,3);
table_row=table(i,nested_table);
LOG(i,:)=table_row;
end
My suboptimal solution is to fill the table before the parfor loop:
But is there a cleaner way?
%create first entry
i=1;
nested_table=table(1,1);
table_row=table(1,nested_table);
LOG=table_row;
%concentrate table with duplicates of this entry
for i=2:10
LOG=[LOG;table_row];
end
%run in parallel
parfor i=2:10
nested_table=table(5,5);
table_row=table(i,nested_table);
LOG(i,:)=table_row;
end
采纳的回答
更多回答(1 个)
Walter Roberson
2021-6-28
The problem is that
LOG=table('Size',[1 2],'VariableTypes',VarTypes);
is creating LOG.Var2 as a 1 x 0 table, but you are trying to assign in a 1 x 2 table.
If you assign a 1 x 2 table to LOG.Var2 before-hand, then the assignments will function properly.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!