Add column in specific number range to available table

4 ビュー (過去 30 日間)
Phuong
Phuong 2024 年 3 月 27 日
編集済み: VBBV 2024 年 3 月 27 日
Hi. I have a 1650x3001 table named A. I want to add a column named B in number from 400 to 2970 that fit the row size, how can I do that?
I have try this but not work, say "Unable to perform assignment because the size of the left side is 1650-by-1 and the size of the right side is 1-by-2571"
A.B(:,1) = (400:2970);
Thanks for your help.

回答 (2 件)

Fangjun Jiang
Fangjun Jiang 2024 年 3 月 27 日
編集済み: Fangjun Jiang 2024 年 3 月 27 日
try this
A.B(:,1) = randi([400,2970],[1650 1])
understand this. Generate a 3x1 matrix with random interger value that is between 1 and 10
randi([1 10],[3,1])
ans = 3x1
1 6 2
help randi

VBBV
VBBV 2024 年 3 月 27 日
A.B(:,1) = (400:2970).'; % use transpose
  1 件のコメント
VBBV
VBBV 2024 年 3 月 27 日
編集済み: VBBV 2024 年 3 月 27 日
@Phuong If i understand your question right, you want to add a column within the range of columns, then you need to use position index and join functions to add the column
pos = 410; % e.g
if pos >=400 & pos <=2970
pos = pos;
else
disp('')
end
B = table(rand(1650,1));
A = join(A,B,'Keys',[pos 1])% join at the specific position

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

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by