how preallocate structure for better memory
Mostra commenti meno recenti
I had created a structure made so:
head.number = 3;
head.pck_rcv = [1 0 0];
heads(2).number = 5;
head(2).pck_rcv = [1 1 0];
and so on.
How can I preallocate a structure?
Risposta accettata
Più risposte (2)
Azzi Abdelmalek
il 22 Set 2012
Modificato: Azzi Abdelmalek
il 22 Set 2012
heads=struct('numbers',zeros(10,1), 'pck_rcv',zeros(10,3))
%then
for k=1:n
heads.numbers(k)=2
heads.pck_rcv(k,:)=[1 2 3]
end
3 Commenti
Alexandra Simpson
il 2 Ott 2017
This should be the top answer IMO :)
Jan
il 2 Ott 2017
@Alexandra: I do not agree. Salvatore asked for a struct array: "head(2).numbers and so on". Azzi's suggestion creates a scalar struct only.
Alexandra Simpson
il 2 Ott 2017
True, I just tried it out and realised it wasn't what I wanted either! Thanks for the response.
Walter Roberson
il 13 Dic 2012
head = struct('number', {3, 5}, 'pck_rcv', {[1 0 0], [1 0 1]})
Categorie
Scopri di più su Variables in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!