Prev: MATLAB LOOP
Next: Read a huge text file in MATLAB
From: Steven Lord on 31 May 2010 23:29 "Vinz Vega" <aspettiamogodot(a)fastwebnet.it> wrote in message news:hu0a9v$2rj$1(a)fred.mathworks.com... > Hello, > > this is a very basic question, I am almost ashamed of asking but I would > really like to know this. > > What I'd like to do is to index the ans I get from querying the size of an > array, employing one line only. > Since I don't know how to do that, I usually do this: > >>>dumb=size(A) >>>dumb(2) If you want the size in the 2nd dimension, ask for the size in the second dimension. dump = size(A, 2) *snip* > As an example, it'd be nice to increase a matrix dimension in a loop based > on its current size, like: > > 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. -- 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: Vinz Vega on 2 Jun 2010 10:58 Hello us, thanks for your help. Your solution doesn't do exactly what I was looking for, I understand I must have not been totally clear. However, your use of 'end' pointed me straight where I wanted to get to. I just didn't know I could address the last index in a dimension with no prior knowledge of the actual size. This surely makes life easier. Other people's posts already addressed my question with: size(A, n_dimension) Once again I didn't know, that 'size' could query one specific dimension at a time, next time I'd better off reading the help page carefully. Now, from: >>dumb=size(A) >>A(dumb(1)+1,:)= something to: >>A(size(A,1)+1,:)= something to: >>A(end+1,:)= something Thanks a lot! This was an amazing support for such a basic question. A first I was afraid no one would reply, while now I am very glad I asked.
From: Vinz Vega on 3 Jun 2010 07:35 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. 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. >>less_convenient=not_enough_room_for_lazyness :)
From: Vinz Vega on 3 Jun 2010 07:44 Hello sscnekro, thanks for replying. >size(A,n_dimension) bang on! embarrassing, but that's exactly what I wanted to know :) >but I am not sure if this really helps to solve the second problem. Do you need just to increase your A matrix alongside the column dimension, or do you want to increase A by concatenating certain column vectors? > A = [A, something(:,1)]; % update A I don't fully get it, aren't the two below equivalent, end equally fine both for increasing along a dimension and concatenating vectors? >>A(:,end+1)=something(:,1) % (i) >>A=[A, something(:,1)] % (ii) Also, >Sorry, typo: something .. < size(A,1) x 1> A = [A, something]; % update A I am far from being familiar with this syntax: >>something .. < size(A,1) x 1> what does that do?
From: Vinz Vega on 3 Jun 2010 07:53
Hello image analyst, thank for replying. > How about > A = [A 1:x] true, and extremely simple as well. ;) |