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 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