How to deal with the structure?

1 回表示 (過去 30 日間)
Benson Gou
Benson Gou 2021 年 6 月 28 日
コメント済み: Benson Gou 2021 年 7 月 7 日
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.
  1 件のコメント
Benson Gou
Benson Gou 2021 年 7 月 7 日
Thanks a lot, Jan. Your code works very well.
Benson

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by