Storing vectors of different length in one single vector
Show older comments
Hello, hope you can help me out here...
I have a for-loop which produces with every loop a vector of values. The vectors it produces can have different lengths (for example, the vector in the first round has 10 values, in the second round 15, in the third 2...). In the end I'd like to store the values of these vectors in one single vector, but i really don't know how to best do it.
Thanks a lot in advance!
Accepted Answer
More Answers (2)
Shiva Arun Kumar
on 9 Feb 2012
0 votes
Walter Roberson
on 9 Feb 2012
The problem isn't so much storing the variable-length information: the problem is in getting the information out afterwards.
You need to store one of:
- the location of the start of each section
- the length of each section
- a unique marker between sections that cannot occur in the data itself
If you choose to store length information, you can store it separately, or you can prefix each segment with the segment length.
For example:
vector_of_data = [];
for k = 1 : whatever
newdata = as appropriate
vector_of_data = [vector_of_data length(newdata) newdata];
end
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!