Matlab running out of memory on code

In summary, the conversation is about a problem with running code in MATLAB that results in running out of data. The code is attempting to find numbers that satisfy a specific condition, but it is not scaling well and causing MATLAB to crash. The suggestion is to use print statements to check the size of arrays as they are being added to the final result.
  • #1
IKnowNada
3
0

Homework Statement


I am trying to run code for the code below but MATLAB runs out of data.

The Attempt at a Solution


NumberOfVariables = 9;
k=NumberOfVariables^2-NumberOfVariables+1;
integers = 0:k-1;
numbers = 1:k-1;
tic
s = combnk(integers,NumberOfVariables);
AllAnswers = [];
for i = 1:size(s,1)
G=combnk(s(i,:),2);
G = [(G(:,1)'-G(:,2)') (G(:,2)'-G(:,1)')];
G = sort(mod(G,k));
if (isequal(G,numbers))
AllAnswers = [AllAnswers;s(i,:)];
end
end
toc

I am trying to find the numbers that satisfy the condition in the code. I want the first AllAnswers that do not contain zeros for NumberOfVariables = 9,10,12,14,17,20?I wrote code for the for loops, but as we all know that doesn't scale favorably in computational time and MATLAB crashes.
 
Physics news on Phys.org
  • #2
I would try placing print statements inside the code to see how large each array is as you keep adding stuff to the all answers array.
 

1. Why does Matlab run out of memory when running my code?

Matlab is a powerful tool for data analysis and modeling, but it has limited memory resources. When running a code that requires a large amount of memory, such as processing large datasets or performing complex calculations, Matlab may run out of memory and produce an error message.

2. How can I prevent Matlab from running out of memory?

One way to prevent Matlab from running out of memory is to optimize your code. This includes avoiding unnecessary variables, using efficient data structures, and preallocating arrays. You can also try breaking your code into smaller chunks and clearing variables from memory when they are no longer needed.

3. Can I increase the memory limit for Matlab?

Yes, it is possible to increase the memory limit for Matlab by changing the Java Virtual Machine (JVM) settings. However, this should only be done if absolutely necessary and with caution, as it can cause other programs on your computer to run slower. It is recommended to consult with a Matlab expert before making any changes to the memory limit.

4. Is my computer's RAM size affecting Matlab's memory usage?

Yes, the amount of RAM (Random Access Memory) on your computer can affect Matlab's memory usage. If your computer has a limited amount of RAM, you may experience more frequent out-of-memory errors when running complex codes. Upgrading your computer's RAM can help improve Matlab's performance.

5. Are there any alternative solutions to running out of memory on Matlab?

If optimizing your code and increasing the memory limit do not solve the issue, there are a few alternative solutions you can try. These include using a computer with more RAM, using a server or cluster for high-memory tasks, or using a different programming language that may be better suited for your specific task.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
802
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
661
  • Engineering and Comp Sci Homework Help
Replies
1
Views
937
  • Engineering and Comp Sci Homework Help
Replies
1
Views
934
  • Engineering and Comp Sci Homework Help
Replies
7
Views
876
  • Engineering and Comp Sci Homework Help
Replies
1
Views
805
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top