0

Is it possible to transfer data in a memory saving way in MATLAB? I.e. is it possible to obtain the following functionality without having the array duplicated in memory

a.b = zeros(1e4);
c = [a.b; ones(1e4)];
a = rmfield(a,'b');
  • Can you please clarify what you're trying to achieve? It seems that nothing is being copied in memory here. – Adriaan Feb 22 '22 at 10:40
  • The data in "a.b" is copied into the variable "c". Before removing the field "b" of the struct "a" the "zeros(1e4)" data is duplicated. So the question is how to avoid this duplication. The motivation for this is when you are assembling matrices into a large sparse matrix where you would like to avoid duplication of data in memory. – user253249 Feb 22 '22 at 11:05
  • I don't think MATLAB handles references like that. In some cases there might be optimisations like copy-on-write but that's an implementation detail. As far as I know everything is always a copy as far as semantics are concerned. – Andras Deak -- Слава Україні Feb 22 '22 at 11:11
  • 2
    The best way to assemble sparse matrices is to do it in one go. keep storing the values and indices and don't build it until you need it – Ander Biguri Feb 22 '22 at 11:14
  • https://stackoverflow.com/q/22817806/2338750 – Stewie Griffin Feb 22 '22 at 11:22
  • Thanks for your replies! I guess I have to rewrite the code entirely in order to assembly my sparse matrix in a memory-efficient way. – user253249 Feb 23 '22 at 12:10

0 Answers0