Storing vectors of different length in one single vector
显示 更早的评论
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!
采纳的回答
更多回答(2 个)
Shiva Arun Kumar
2012-2-9
0 个投票
Walter Roberson
2012-2-9
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
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!