Prev: MATLAB LOOP
Next: Read a huge text file in MATLAB
From: Steven Lord on 3 Jun 2010 22:14 "Vinz Vega" <aspettiamogodot(a)fastwebnet.it> wrote in message news:hu841b$ktr$1(a)fred.mathworks.com... > Thanks Steve, > >> dump = size(A, 2) > > bang on! ;) > >> > for n=1:x >> > A(length(A)+1)= n > >> Don't. If at all possible, preallocate your A matrix to be the correct >> size BEFORE you loop, rather than growing it in the loop. It's more >> efficient. > > Pseudo error messages in the editor always warn me about it. Those are from Code Analyzer -- generally red messages indicate you've got a problem that will prevent your code from running, while orange messages generally indicate something that may affect the efficiency of your code. > I always look away though, since the syntax runs anyways. I don't really > know how to preallocate and read how to. It seems to me I somehow need to > have prior knowledge of how much I want to grow the array, which somewhat > makes it less convenient from the coding side. True, but it can have a decent effect on the efficiency of your code. function timingtest28 clear A; % Not really necessary in this case tic for rows = 1:1000 for cols = 1:1000 A(rows, cols) = rows+cols; end end toc clear B; % See above tic B = zeros(1000, 1000); % Preallocation! for rows = 1:1000 for cols = 1:1000 B(rows, cols) = rows+cols; end end toc When I tried this a few times using release R2010a, I received these results: >> timingtest28 Elapsed time is 5.061569 seconds. Elapsed time is 0.029336 seconds. >> timingtest28 Elapsed time is 5.095786 seconds. Elapsed time is 0.031116 seconds. >> timingtest28 Elapsed time is 5.126281 seconds. Elapsed time is 0.029156 seconds. Now if you're just running this once or twice, it may not be so significant. If you're running this hundreds or thousands of times? Those 5 seconds can add up. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
From: sscnekro on 4 Jun 2010 09:01 Hi Vinz Vega, > I am far from being familiar with this syntax: > >>something .. < size(A,1) x 1> > what does that do? Sorry, that was not meant as a reference to syntax. From your question it is clear to me that I was probably confusing. As you already figured out, size(A,1) returns the first dimension, ie row, of the A matrix. By < dim x dim > I referred to, loosely speaking, the way Matlab describes variables in the Workspace window (I hope you see the same with your Matlab release, I have R2008b). In fact, a 2-D A matrix is displayed as A <dimension_row x dimension_column double> Mixing it together, by "something .. < size(A,1) x 1>" I meant a vector (in Matlab language matrix) named something, of the length equal to number of rows in A. Based on some of your difficulties, I 'd suggest, you may read and work through the 2nd chapter of http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/getstart.pdf It won't take much time and I fancy it may be more useful for you at the moment than trying to read Matlab Help documentation. Best of luck.
From: Vinz Vega on 5 Jun 2010 20:09 Thanks Steve! Now my editor displays a nice green light, hadn't seen that for ages. The computation behind my code is so lightweight at the moment that preallocating doesn't make it sensibly more efficient, but I will bear it in mind and keep doing it nonetheless. one day it will make a difference. thanks again, vinz.
From: Vinz Vega on 7 Jun 2010 21:23 Thanks sscnekro, very kind. I'll follow your advice and work through the guide. a bit of revision couldn't hurt. thanks again, vinz.
From: sscnekro on 8 Jun 2010 06:03
> I'll follow your advice and work through the guide. a bit of revision couldn't hurt. No, no, sorry, really. I remember your thread, bcs after I posted it I got a stupid feeling that you are much longer with programming than me and *should have not* posted, etc. I needed to get acquainted with five programming languages recently (whithout previous prog. experience), with two I made the mistake not to read systematically the getting started stuff, but immediately hlp docs. I could do the things I needed to do, but... |