How to create an array which changes with each iteration of a loop?

How to create an array which changes with each iteration of a for loop? for example;
polyX(i)=horzcat(X2(i),X1(i+1),V(i+1));
polyX is an array.

 採用された回答

Star Strider
Star Strider 2016 年 11 月 12 日

0 投票

It is not certain what you want to do. We don’t know what scalars or arrays you’re concatenating, so I would create ‘polyX’ as a cell array, since cell arrays are much more tolerant of changing dimensions than matrices:
polyX{i} = horzcat(X2(i),X1(i+1),V(i+1));
Note the curly braces ‘{}’ denoting that ‘polyX’ is now a cell array. See the documentation on Cell Arrays for details.

4 件のコメント

KalMandy
KalMandy 2016 年 11 月 12 日
Thank you Star. is it possible that I can use a similar cell array polyY{i} and write,
area(i)=polyarea( polyX{i}, polyY{i})
Star Strider
Star Strider 2016 年 11 月 12 日
My pleasure.
That will probably work. (I caution ‘probably’ because I don’t know what ‘polyX’ and ‘polyY’ are.)
Also, please re-name your ‘area’ variable to something else, since area is a function (similar to patch) that fills a plotted area with a colour. (This is known as ‘overshadowing’ and will cause serious problems if you need to use the area function later in your code, and you may want to use it.) Instead, name it ‘p_area’ or something that describes what it is, and also has a name different than MATLAB built-in functions.
KalMandy
KalMandy 2016 年 11 月 12 日
Thank you very much, Your explanations are very easy to follow!
Star Strider
Star Strider 2016 年 11 月 12 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

質問済み:

2016 年 11 月 12 日

コメント済み:

2016 年 11 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by