# How to deal with the structure?

2 views (last 30 days)
Benson Gou on 28 Jun 2021
Commented: Benson Gou on 7 Jul 2021
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
Benson Gou on 30 Jun 2021
Hi, Rik,
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 on 30 Jun 2021
Edited: Jan on 30 Jun 2021
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.
Benson Gou on 7 Jul 2021
Thanks a lot, Jan. Your code works very well.
Benson