How to deal with the structure?

1 次查看(过去 30 天)
Dear All,
I have a struct which saves a number of records. I have the following codes which takes more time than expected.
Resstd2 = [];
resstd20 = [];
treestruct2 = [];
nnzV = find(v);
m1 = length(TreeStruct2);
temp = [];
for i = 1 : m1
vblockid = TreeStruct2(i).treestruct.vblockID;
solvedvid = v(vblockid);
resstd2 = TreeStruct2(i).Resstd;
if nnz(solvedvid) == 0
if resstd2(3) < voltagelimit
treestruct2 = [treestruct2; TreeStruct2(i).treestruct];
Resstd2 = [Resstd2; resstd2];
else
resstd20 = [resstd20; resstd2];
end
end
temp = [temp; resstd2(3)];
end
It seems to me that using structures takes long time. Does anyone have the experience in cell or table? Do you think using cells or tables will be faster than using structures?
Thanks.
Benson
  3 个评论
Rik
Rik 2021-6-30
Your edit doesn't seem to address the issue I raised in my comment. Without a good description of what you want, the only thing we can say is not to use a dynamic expansion.
If you want help with your code specifically, you should make sure we can run it.
Resstd2 = [];
resstd20 = [];
treestruct2 = [];
nnzV = find(v);
Unrecognized function or variable 'v'.
Benson Gou
Benson Gou 2021-6-30
Hi, Rik,
Thanks a lot for your reply.
The data is big and is hard to display the data in my question. Is it possible to attached the mat file in my question?
Thanks.
Benson

请先登录,再进行评论。

采纳的回答

Jan
Jan 2021-6-30
编辑:Jan 2021-6-30
Do not let arrays grow iteratively because this causes exponentially growing computing times. Example:
x = [];
for k = 1:1e6
x(k) = k;
end
Although the final array has only 8MB (8 bytes per double), Matlab has to allocate a new array in each iteration and copy the old values. In total this allocates sum(1:1e6)*8 = 4TB!
m1 = length(TreeStruct2);
Resstd2 = zeros(m1, 1);
Resstd2_i = 0;
resstd20 = zeros(m1, 1);
resstd20_i = 0;
treestruct2_m = false(m1, 1);
% nnzV = find(v); % Not used
temp = zeros(m1, 1);
for i = 1 : m1
vblockid = TreeStruct2(i).treestruct.vblockID;
solvedvid = v(vblockid);
resstd2 = TreeStruct2(i).Resstd;
if ~any(solvedvid) % Faster than nnz==0
if resstd2(3) < voltagelimit
treestruct2_m = true;
Resstd2_i = Resstd2_i + 1;
Resstd2(Resstd2_i) = resstd2;
else
resstd20_i = resstd20_i + 1;
resstd20(resstd20_i) = resstd2;
end
end
temp(i) = resstd2(3);
end
Resstd2 = Resstd2(1:Resstd2_i); % Crop unused elements
resstd20 = resstd20(1:resstd20_i); % Crop unused elements
treestruct2 = cat(1, TreeStruct2(treestruct2_m).treestruct);
Maybe Resstd2 and resstd20 need more dimension. Without provided input data, e.g. created by RAND() I cannot guess this. Adjust the dimensions on demand.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by