How to deal with the structure?

3 visualizaciones (últimos 30 días)
Benson Gou
Benson Gou el 28 de Jun. de 2021
Comentada: Benson Gou el 7 de Jul. de 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
  3 comentarios
Rik
Rik el 30 de Jun. de 2021
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 el 30 de Jun. de 2021
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

Iniciar sesión para comentar.

Respuesta aceptada

Jan
Jan el 30 de Jun. de 2021
Editada: Jan el 30 de Jun. de 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.
  1 comentario
Benson Gou
Benson Gou el 7 de Jul. de 2021
Thanks a lot, Jan. Your code works very well.
Benson

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by