Using repelem to vertially concatonate non-numeric variable

3 ビュー (過去 30 日間)
Emu
Emu 2023 年 11 月 9 日
回答済み: Steven Lord 2023 年 11 月 9 日
I have a variable in format 1001_1_1 that changes in a loop. I want to replicate and vertcat this by different amounts each loop, to a new variable eg
1001_1_1
1001_1_1
3005_3_5
3005_3_5
3005_3_5
and so on.
Ive tried using repelem:
new_var = repelem(name, n)
new_var_all = [new_var_all; new_var];
but if e.g. n=3, this is the result: 111000000666___111___333
Please help!

採用された回答

Star Strider
Star Strider 2023 年 11 月 9 日
I am not certain what you want to do, however the repmat function might be a better choice, since it allows the dimensions to be specified.
v = '1001_1_1';
new_var = repmat(v, 3, 1)
new_var = 3×8 char array
'1001_1_1' '1001_1_1' '1001_1_1'
.
  1 件のコメント
Atsushi Ueno
Atsushi Ueno 2023 年 11 月 9 日
n = 3;
new_var_all = [];
name = {'1001_1_1';'3005_3_5';'6007_7_9'};
for k = 1:size(name)
new_var = repmat(name{k}, n, 1);
new_var_all = [new_var_all; new_var];
end
new_var_all
new_var_all = 9×8 char array
'1001_1_1' '1001_1_1' '1001_1_1' '3005_3_5' '3005_3_5' '3005_3_5' '6007_7_9' '6007_7_9' '6007_7_9'

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

その他の回答 (3 件)

Stephen23
Stephen23 2023 年 11 月 9 日
編集済み: Stephen23 2023 年 11 月 9 日
".. repmat function might be a better choice, since it allows the dimensions to be specified."
As does REPELEM:
repelem('1001_1_1',3,1)
ans = 3×8 char array
'1001_1_1' '1001_1_1' '1001_1_1'

Emu
Emu 2023 年 11 月 9 日
Ahhh thank you :)

Steven Lord
Steven Lord 2023 年 11 月 9 日
Note that the for loop approach from @Atsushi Ueno works if the "pieces" of the names are the same length all the way down the list. But if you had:
name = {'1001_1_1';'3005_3_5';'6007_7_10'}; % 10 not 9
you'd receive an error. In this case, I'd use a string array and repmat or repelem as @Star Strider and @Stephen23 suggested.
names = ["1001_1_1";"3005_3_5";"6007_7_10"];
R = repelem(names, 3, 1)
R = 9×1 string array
"1001_1_1" "1001_1_1" "1001_1_1" "3005_3_5" "3005_3_5" "3005_3_5" "6007_7_10" "6007_7_10" "6007_7_10"
If you need the elements as char vectors (because a function you're trying to use only supports char vectors or would need to be modified to support string arrays, like if it uses concatenation to combine the name with something else) you can call char.
c = char(R(8))
c = '6007_7_10'
Another possibility, if you're trying to assemble these names from all combinations of the "pieces", is to use combinations and join.
C = combinations([1001, 3005, 6007], [1, 3, 7], [1, 5, 10]);
join(string(C.Variables), "_")
ans = 27×1 string array
"1001_1_1" "1001_1_5" "1001_1_10" "1001_3_1" "1001_3_5" "1001_3_10" "1001_7_1" "1001_7_5" "1001_7_10" "3005_1_1" "3005_1_5" "3005_1_10" "3005_3_1" "3005_3_5" "3005_3_10" "3005_7_1" "3005_7_5" "3005_7_10" "6007_1_1" "6007_1_5" "6007_1_10" "6007_3_1" "6007_3_5" "6007_3_10" "6007_7_1" "6007_7_5" "6007_7_10"

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by