MATLAB Array Pre-allocation help

In summary: To preallocate a string, you can use the repmat() function to create a string with a specific number of characters, and then use indexing to fill it in a loop. For example:myString = repmat('a', 10000, 1); % creates a string of 10000 'a' charactersfor i = 1:10000 myString(i) = 'b'; % fills the string with 'b' characters in a loopend
  • #1
hoffmann
70
0
Hi everyone. I'm working on a piece of code and I'm having a bit of trouble with array pre-allocation. Here it is:

function [react_in1 react_in2 react_out1 react_out2]=oneVtwoXone(masses,prec);

prec = 1e-4;
N=length(masses);
masses=[0;masses]; % adding dummy metabolite with zero mass
masses=masses(:);

%mass2=masses*ones(size(masses))'+ones(size(masses))*masses';
%mass2=mass2(:);

mass2=zeros(N*(N+1)/2,1); % list of mass pairs
met1=mass2; % list of the first metabolite in a pair
met2=mass2; % same for the second metabolite
k=0;
for i=1:N
for j=i:N
k=k+1;
met1(k)=i;
met2(k)=j;
mass2(k)=masses(i)+masses(j);
end
end

disp(['Number of distinct pairs is' length(mass2) ])

[mass2, ord]=sort(mass2); % sorted mass pairs
met1=met1(ord); % metabolites in the same order as sorted mass pairs
met2=met2(ord);

dm=diff(mass2);

while(length(find(dm <= mass2(1:length(dm))*prec)) > 0) % while have metabolite pairs that have about the same mass
ind=find(dm<mass2(length(mass2)-length(dm)+1:end)*prec);




react_in1=[react_in1 met1(ind)];
react_in2=[react_in2 met2(ind)];
react_out1=[react_out1 met1(ind+1)];
react_out2=[react_out2 met2(ind+1)];


dm = dm(1:end-1) + dm(2:end);

end

disp(['Number of conforming reactions is' length(react_in1) ])

The source of my trouble is in the second while loop. The react_in1 etc. takes up too much memory and I need to figure out how to preallocate a matrix to hold values approximately 10000, and how to delete the excess zeros if there is any unused space.

Can anyone help?
 
Physics news on Phys.org
  • #2
To preallocate a large matrix, just use the zeros() function. Why do you think your matrices are taking up too much memory? Preallocating them will make your code faster, but won't make them take up any less memory.

- Warren
 
  • #3
well actually I've found that preallocating takes up a significantly less amount of time. i have such a huge data set of 10000 values so my code runs faster. here's what I'm doing now:

while(length(find(dm <= mass2(1:length(dm))*prec)) > 0) % while have metabolite pairs that have about the same mass
ind=find(dm<mass2(length(mass2)-length(dm)+1:end)*prec);


react_in1=zeros(10000, 1); % preallocate a new array for all four reactants
react_in2=zeros(10000, 1);
react_out1=zeros(10000, 1);
react_out2=zeros(10000, 1);
count = 0;

for ind=1:10000

react_in1(ind)=met1(ind);
react_in2(ind)=met2(ind);
react_out1(ind)=met1(ind+1);
react_out2(ind)=met2(ind+1);
end


dm = dm(1:end-1) + dm(2:end);

end

how do i truncate the unused zeros?
 
  • #4
An array of 10,000 values is actually quite SMALL.

You can use the slice operators (like dm(1:10)) to "truncate" an array to whatever size you want.

- Warren
 
  • #5
how can I preallocate long strings? in other words, How can I rapidly create a string of a certain length and then fill it in a loop?
 

1. What is array pre-allocation in MATLAB?

Array pre-allocation in MATLAB is the process of reserving memory space for an array before filling it with values. This helps improve the efficiency and speed of code execution, especially when dealing with large arrays.

2. Why is array pre-allocation important in MATLAB?

Array pre-allocation is important in MATLAB because it reduces the need for frequent memory reallocation during array creation, which can significantly slow down code execution. It also helps avoid memory fragmentation, resulting in more efficient memory usage.

3. How do you pre-allocate an array in MATLAB?

To pre-allocate an array in MATLAB, you can use the "zeros", "ones", or "empty" functions followed by the desired array size. For example, "A = zeros(3,4)" will create a 3x4 array of zeros and reserve memory space for it.

4. Can you pre-allocate non-numeric arrays in MATLAB?

Yes, you can pre-allocate non-numeric arrays in MATLAB. The "cell" function can be used to pre-allocate an array of cells, and the "string" function can be used to pre-allocate an array of strings. These functions also require specifying the array size.

5. Is array pre-allocation always necessary in MATLAB?

No, array pre-allocation is not always necessary in MATLAB. It is most beneficial when dealing with large arrays or when the array size is known beforehand. In some cases, pre-allocation may not make a significant difference in code execution time, so it is important to consider the specific scenario before implementing it.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
5K
Replies
1
Views
739
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Advanced Physics Homework Help
Replies
12
Views
2K
Back
Top