首页 > 解决方案 > 在工作区中保存 parfor 循环数据(Matlab)

问题描述

晚上好,我可以帮我写一个脚本吗?我有一个嵌套在 for 循环中的 parfor 循环。目标是迭代一组数据,该数据集由早期 parsim simulink 分析生成的 10 个数据子集组成(它被标记为1x10 SimulationOutput)。每个数据子集有 24 行深,列的长度可变(通常约为 200,000 到 300,000 列数据)。该过程的一部分是在每个数据集中找到最大值或最小值。完成后,将其放入表中,将数据附加到该表中。理想情况下,最后我应该有一个 6x10 的桌子。请参阅下面的代码:

% Run Time
tic

% Preallocate memory to increase speed

b=zeros(24,1); %Make space for this array.
c=zeros(500000,1);
d=zeros(500000,1);
e=zeros(500000,1);
f=zeros(500000,1);
g=zeros(500000,1);
h=zeros(500000,1);
%table=[];

for j = 1:length(out(1,:)) %iterate over each run

    parfor i = 1:length(out(1,j).PN.time) % Set length of vector
        b=out(1,j).PN.signals.values(:,i); % Find the values to work on
        c(i)=b(19,:); % Distance to target (m)
        d(i)=b(20,:); % Lat. Accelerations, integrated twice (m)
        e(i)=b(21,:); % Long. Acceleration, integrated twice (m)
        f(i)=b(22,:); % Lat. Guidance Error
        g(i)=b(23,:); % Long. Guidance Error
        h(i)=b(24,:); % time to target (sec)
   
    end
   
    %For c_min, there's extranous zeros popping up, exclude them
    tc = c;
    tc(tc <= 0) = nan;
    [c_min, I_1] = min(tc);
    %    [c_min,I_1]=min(c(c>0)); % Collect the closest missile/target approach (most 
    critical value)
    
    [d_max,I_2]=max(d); % We need to find the max value per run, but wish for the min value 
    %over all runs.
    
    [e_max,I_3]=max(e); % We need to find the max value per run, but wish for the min value 
    %over all runs.
    
    [f_min,I_4]=min(f); % We just want the minimum value here.
    
    [g_min,I_5]=min(g); % We just want the minimum value here.

    [h_max,I_6]=max(h); % The minimum time is 2nd most critical value, after distance to 
    %target.

    table(:,j)=[ c_min d_max e_max f_min g_min h_max]; %d_max e_max f_min g_min h_max

end

toc

我遇到的问题是,虽然我可以在表中的正确位置输入正确的数据集,但如果我设置了一个常量 j 值(例如:如果 j = 7,那么表中的第 7 列将获得正确的数据) 我似乎无法正确输入所有值。我的意思是,输出的表(6x10)将具有跨列的重复值,另一列中的一列的值,等等)。就好像脚本不能再区分列,所以值随处可见。

如果有人有任何建议,我将不胜感激。谢谢,

标签: matlabfor-loopnanparfor

解决方案


推荐阅读