How can I avoid the fill-in problem?

I would like to know if there is a way to avoid the fill-in problem adding some code lines to the script. The thing is that when that happens to me the computer doesn't respond at all. I can't even open msconfig to stop the program from there, so I just end up turning off the computer. If this happened to me in the middle of an exam, I don't think it would be very pleasant.

6 Comments

I agree, it would not be pleasant.
I don’t understand the ‘fill-in problem’.
What do you mean by it?
Can you give an example?
I don’t know what sort of applications you’re programming. The only time I’ve encountered something like this has been when I created a vector incorrectly by mistyping the increment and ended up with one that was so long it crashed MATLAB.
Using the linspace function to create vectors will prevent this. Also, check the sizes of matrices before you use them in a calculation.
Now I know what happened. I was working with two long vectors. I intended to do a scalar product (1x20480)*(20480x1) but the real operation was (20480x1)*(1x20480), which gave a square matrix of dimensions (20480x20480).
That was in fact easy to spot, but what would happen if this operation was hidden among many lines of code? Isn't there anything one can do to avoid this situation? Not even ctrl+c worked.
The only solution I’ve found is going to the Task Manager (Windows) and closing MATLAB from there, since it also won’t respond to clicking the red x at the top right of the MATLAB window.
I agree, it would be nice if there was some sort of warning. Put in an enhancement request for something that monitored memory usage, and could have the user set a threshold for matrix size.
The time I most recently encountered it was when I was doing a simple linear regression with long vectors. As the first column (a column vector of ones used to calculate the intercept), I mistakenly typed ‘ones(length(x))’ instead of ‘ones(length(x),1)’. That essentially crashed MATLAB, at least by my definition.
Many thanks to the three of you.

Sign in to comment.

Answers (2)

Jan
Jan on 26 Apr 2015
Edited: Jan on 26 Apr 2015
You can limit the size of created arrays in Matlab 2015a. See http://www.mathworks.com/help/matlab/release-notes.html "Array Size Limit" (Sorry, this link will not work anymore when R2015b is released)
With older Matlab versions you cannot prevent that the user provides insane commands. And even with the newest version a stubborn or careless user can kill the machine.

1 Comment

I missed that when I read the Release Notes. Thanks.
I hope the functionality still exists in R2015b.
The link may work if the documentation for previous versions remains. It just won’t redirect to the R2015b documentation.

Sign in to comment.

John D'Errico
John D'Errico on 26 Apr 2015
I'm sorry, but there is simply no substitute for thinking about what you are doing. Look at what you have written before you hit enter, before you cause the code to execute. If you are unsure about it, test out a code fragment on a small problem.
Write modular code. Test each module separately. Then when you put it all together, you can be confident it will run smoothly.
The fact is, once you get used to being careful in your coding, code gets easy to write, because you KNOW that it will work as you have written it.

Categories

Community Treasure Hunt

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

Start Hunting!