Prev: Text File With Header
Next: FTP problem accessing NASA
From: SM on 11 May 2010 14:05 Hi everybody, I want to create a matrix where some elements is added to the former one while iterating by j. (a+((j-1)/j)) assume a=1:1:10 for j=1:2 b=sort([b(1:j-1,:) a+[(j-1)/j]]) end where I get: b=[1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000 10.5000] when I set the j to values other than 2, then I get an error. Would you please help me with this?
From: someone on 11 May 2010 14:19 "SM " <khanmoradi(a)gmail.com> wrote in message <hsc692$msa$1(a)fred.mathworks.com>... > Hi everybody, > I want to create a matrix where some elements is added to the former one while iterating by j. (a+((j-1)/j)) > > assume a=1:1:10 > for j=1:2 > b=sort([b(1:j-1,:) a+[(j-1)/j]]) > end > > where I get: > > b=[1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000 10.5000] > > when I set the j to values other than 2, then I get an error. > > Would you please help me with this? In your above code, when j=1 you have: b(1:j-1,:) = b(1:0,:) = ??? (You treid to access a 0th element which MATLAB can't do.)
From: us on 11 May 2010 14:54 "SM " > for j=1:2 > b=sort([b(1:j-1,:) a+[(j-1)/j]]) > end > when I set the j to values other than 2, then I get an error. > > Would you please help me with this? a hint: - the error is here b(1:j-1,:) which accesses matrix B of dynamically changing size J-1 x N and matrix B of size 1 x N b=nan(3,1); % <- or ANY other pre-allocation(!)... % observe for j=1:3 disp(j); [b(1:j-1,:),1:4] end %{ 1 % <- works because b(1:0,:) <=> empty(!) ans = 1 2 3 4 2 % <- works because b(1,:) <=> a ROW vec ans = NaN 1 2 3 4 3 % <- because b(1:2,:) is a 2x1 and cannot be concatenated with A: 1x4 ??? Error using ==> horzcat CAT arguments dimensions are not consistent. %} us
From: SM on 11 May 2010 18:49 Actually, I already knew that the problem is from b(1:j-1,:). I tried, I couldn't fix the problem. actually, I have no idea how to do it anymore.:(... Any help is appreciated
From: dpb on 11 May 2010 20:22
SM wrote: > Actually, I already knew that the problem is from b(1:j-1,:). I tried, I > couldn't fix the problem. actually, I have no idea how to do it > anymore.:(... > Any help is appreciated More specific definition of what you're actually wanting might help a little... But, if you're going to try to concatenate a vector onto an array, you're going to have to do it such that it has the proper dimensions as us has shown -- either a row or column can be added but it has to have the same length as the existing matrix row or column, respectively, to which you want to add it to maintain a rectangular array. You're changing sizes as well. The only way to do _that_ is to use cell array where each cell can contain a different-sized array/vector. I'm thinking you haven't clearly defined your requirements for starters... -- |