Adding Rows with Specific Values to Existing Variables

Hello,
I have a 7x1 variable ("X1") to which I need to add a row at the beginning (cell value = 0) and a row at the end (cell value = 0). Obviously, I could do this manually, but I have many variables this needs to be done to.
My noodle is fried today. Apologies.

 採用された回答

Star Strider
Star Strider 2016 年 10 月 13 日
編集済み: Star Strider 2016 年 10 月 13 日

0 投票

The easiest way is to concatenate zeros at the beginning and end of ‘X1’:
X1 = randi(9, 7, 1); % Create Data To Test Code
Result = [0; X1; 0];

3 件のコメント

balsip
balsip 2016 年 10 月 13 日
Star Strider, that doesn't quite have the intended results. Before, by 7x1 variable was:
X1 =
59.9594
84.8451
59.9945
0.0000
-59.9945
-84.8451
-59.9594
Adding your code:
X1 = randi(9,7,1);
Result = [0; X1; 0];
Results in the following 9x1:
0
8
2
4
9
8
9
6
0
I need to keep the values in the original 7x1 but add first and last cells with 0 values. Should be:
0
59.9594
84.8451
59.9945
0.0000
-59.9945
-84.8451
-59.9594
0
James Tursa
James Tursa 2016 年 10 月 13 日
編集済み: James Tursa 2016 年 10 月 13 日
The X1 = randi(9, 7, 1) was just to get some sample data. You can eliminate that line since you already have X1 with your actual data. Just do this:
Result = [0; X1; 0];
Star Strider
Star Strider 2016 年 10 月 13 日
Thank you, James!
Ambiguity now resolved (I hope).

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGet Started with MATLAB についてさらに検索

質問済み:

2016 年 10 月 13 日

コメント済み:

2016 年 10 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by