saving multiple vectors with different lengths in one matrix

Hi everyone,
For a research project at my university I need to make histograms of several runs of an ecological model.
On my 'main' matix M I keep track of trees, on another matrix 'M3' I keep track of their age.Since there is some stochastic elementes in the model I need to run it for 25 repetitions, I do that using 'for rep = 1:25', then my code, then 'end'.
Now I want to save the age of the trees each repetition in order to make an average histogram of the tree-age for the entire run (25 repetitions), but I can't save it in a matrix because of their different lengths. How can I cicumvent this problem?
Thanks! Marcus Westerman

 採用された回答

Matt Fig
Matt Fig 2011 年 5 月 17 日

26 投票

You can use cell arrays, or pad with zeros. For example:
v1 = [2 3 4 5];
v2 = [2 3 4 5 6 7];
% Make padded array. Could use rows or columns...
M = zeros(2,6);
M(1,1:length(v1)) = v1;
M(2,1:length(v2)) = v2;
% Or use a cell array.
M2{1} = v1;
M2{2} = v2;

3 件のコメント

jacks jones
jacks jones 2012 年 11 月 27 日
The above cell array example also worked well for me to save my data from a for loop. However, I need to plot the cell array data with inpolygon function, I drew a rectangle, but I am not sure how to use the cell array and inpolygon function together. I am testing whether certain values are in the rectangle or not.
plot(M2{1,1},M2{1,2})
hold on
plot(M3{1,1},M3{1,2})
%
Francisco Angel
Francisco Angel 2018 年 12 月 5 日
For anyone interested you can access elements like this:
M2{1}(1), M2{1}(2) , etc.
Mansoor Ashraf
Mansoor Ashraf 2019 年 3 月 13 日
The first solution is awesome. Thanks.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2011 年 5 月 17 日

コメント済み:

2019 年 3 月 13 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by