1

I want to sum up several vectors of different size in an array. Each time one of the vectors drops out of my program, I want to append it to my array. Like this:

array = [array, vector];

In the end I want to let this array be the output of a function. But it gives me wrong results. Is this possible with MATLAB?

Thanks and kind regards, Damian

Damian
  • 139
  • 3
  • 13
  • Yes this is possible. But a little more information would be useful: 1) Do you know beforehand how many vectors will drop out? 2) Are they row vectors or column vectors? If you know beforehand how many will drop out, this question is possibly a duplicate, see my (and Rody's) answer to a very similar question [here](http://stackoverflow.com/questions/12506159/recovering-vector-in-matlab-for-loop/) – Colin T Bowers Sep 21 '12 at 06:56
  • 5
    You can try to use [cell](http://www.mathworks.com/help/matlab/ref/cell.html) – chaohuang Sep 21 '12 at 07:07
  • Hi Colin, the vectors are colvectors (row vectors really wouldn't like the append as shown above, would they?). But I do not know their number in advance, therefore I want to do it with appeding. Thanks, Damian – Damian Sep 21 '12 at 07:15
  • 1
    Can you provide the key parts of the actual code you are using, and explain exactly how your output is currently wrong? For general info on appending outputs of a length that is not known in advance, see [this other question](http://stackoverflow.com/questions/1548116/matrix-of-unknown-length-in-matlab) . – cjh Sep 21 '12 at 21:07
  • Hi Damian. If you are satisfied with my answer, then please mark the question answered by clicking the tick icon next to my answer. If you are not satisfied with the answer then it would be useful if you could let me know why so I can attempt to improve it. Also, I found the link provided by @cjh to be very interesting (thanks cjh) so I would second cjh that it is worth checking out. – Colin T Bowers Sep 22 '12 at 02:19

1 Answers1

5

Okay, given that we're dealing with column vectors of different size, you can't put them all in a numerical array, since a numerical array has to be rectangular. If you really wanted to put them in the numerical array, then the column length of the array will need to be the length of the longest vector, and you'll have to pad out the shorter vectors with NaNs.

Given this, a better solution would be, as chaohuang hinted at in the comments, to use a cell array, and store one vector in each cell. The problem is that you don't know beforehand how many vectors there will be. The usual approach that I'm aware of for this problem is as follows (but if someone has a better idea, I'm keen to learn!):

UpperBound = SomeLargeNumber;
Array = cell(1, UpperBound);
Counter = 0;
while SomeCondition
    Counter = Counter + 1;
    if Counter > UpperBound
        error('You did not choose a large enough upper bound!');
    end
    %#Create your vector here
    Array{1, Counter} = YourVectorHere;

end
Array = Array(1, 1:Counter);

In other words, choose some upper bound beforehand that you are sure you won't go above in the loop, and then cut your cell array down to size once the loop is finished. Also, I've put in an error trap in case you're choice of upper bound turns out to be too small!

Oh, by the way, I just noted in your question the words "sum up several vectors". Was this a figure of speech or did you actually want to perform a sum operation somewhere?

Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89