Adding a new variable to a table and assigning a value

I am working with a table with multiple variables (called T3) - dimensions are 1349x26. This is one station (CTD instrument).
I want to add a new variable called Station ID and define it's value as 3 for all 1349 rows.
I know how to do it in the table, but I am confused how to write it in a code.
Example of what I did:
Station ID = 3;
T3 = addvars(T3, Station ID, 'Before', 'VarName1');
I was using the addvars function but I kept getting an error because Station ID's number of rows, does not match the table's height.
How can I define Station ID's dimensions and fill all 1349 rows with the same value?

 採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 22 日
編集済み: Dyuman Joshi 2023 年 9 月 22 日

0 投票

"How can I define Station ID's dimensions and fill all 1349 rows with the same value? "
There are many ways of doing that -
%Number of rows in the table
s = size(T3,1);
%Method 1
Station_ID = 3*ones(s,1);
%Method 2
Station_ID = repelem(3,s,1);
%Method 3
Station_ID = 3 + zeros(s,1);

2 件のコメント

Rachel
Rachel 2023 年 9 月 22 日
Thank you!
Just a clarification, the 1 in s= size(T3,1) means just for one column correct?
Dyuman Joshi
Dyuman Joshi 2023 年 9 月 22 日
編集済み: Dyuman Joshi 2023 年 9 月 22 日
No, the 1 is used to get the size of the 1st dimension of T3, as rows are the 1st dimension of matrices.
Here's an example to give you more idea about it -
T3 = zeros(2,3,5);
%Get the size of T3
size(T3)
ans = 1×3
2 3 5
%Get the size of 1st dimension of T3
size(T3,1)
ans = 2
%Get the size of 2nd dimension of T3
size(T3,2)
ans = 3
%Get the size of 3rd dimension of T3
size(T3,3)
ans = 5
Refer to the documentation of size for additional information.
Also, @Rachel, if my answer solved your problem, please consider accepting it.

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

その他の回答 (0 件)

カテゴリ

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

タグ

質問済み:

2023 年 9 月 22 日

編集済み:

2023 年 9 月 22 日

Community Treasure Hunt

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

Start Hunting!

Translated by