0

我有一个带有一组按时间顺序排列的序列号的数组和另一个带有与数值相关联的随机序列号的源数组。该代码在 MATLAB 中创建一个新元胞数组,其中一列中的序列号完全按时间顺序排列,如果序列号在两个原始源数组中匹配,则在下一列中插入关联的数值。如果他们不这样做,代码只会复制先前的关联值,直到有新的匹配。

j = 1;
A = {random{1:end,1}};
B = cell2mat(A);
value = random{1,2};
data = cell(length(serial), 1);
data(:,1) = serial(:,1);

h = waitbar(0,'Please Wait...');
steps = length(serial);

for k = 1:length(serial)

    [row1, col1, vec1] = find(B == serial{k,1});
    tf1 = isempty(vec1);

    if (tf1 == 0)
        prices = random{col1,2}; 
        data(j,2) = num2cell(value);
        j = j + 1;
    else
        data(j,2) = num2cell(value);
        j = j + 1;
    end

    waitbar(k/steps,h,['Please Wait... ' num2str(k/steps*100) ' %'])

end

close(h);

目前,代码的运行时间约为 4 小时。我想让这段代码运行得更快。请提出任何这样做的方法。

更新

source input (serial)
1
2
3
4
5
6
7

source input (random)
1    100
2    105 
4    106
7    107

desired output (data)
SR No           Value
1               100
2               105
3               105
4               106
5               106
6               106
7               107
4

2 回答 2

4

首先,运行 MATLAB 分析器(参见 'doc profile')并查看大部分执行时间发生在哪里。

其次,不要在每次迭代时更新等待栏>特别是如果 serial 包含大量(> 100)元素。

执行以下操作:

if (mod(k, 100)==0) % update on every 100th iteration
     waitbar(k/steps,h,['Please Wait... ' num2str(k/steps*100) ' %'])
end
于 2013-07-30T07:02:22.060 回答
0

Some points:

Firstly it would help a lot if you gave us some sample input and output data.

Why do you initialize data as one column and then fill it's second in the loop? Rather initialize it as 2 columns upfront: data = cell(length(serial), 2);

Is j ever different from k, they look identical to me and you could just drop both the j = j + 1 lines.

tf1 = isempty(vec1); if (tf1 == 0)... is the same as the single line: if (!isempty(vec1)) or even better if(isempty(vec1)) and then swap the code from your else and your if.

But I think you can probably find a fast vecotrized solution if you provide some (short) sample input and output data.

于 2013-07-30T10:48:58.067 回答