Can MATLAB duplicate cell array entries without creating cell within cells?

12 ビュー (過去 30 日間)
Brad
Brad 2018 年 4 月 25 日
コメント済み: Brad 2018 年 4 月 25 日
I'm attempting to duplicate cell array data (from 1 cell array) and place in a different cell array without creating cells within cells. I'm using the following code:
% Some cell array data
data = [ 'ALC238Tires'; 'ALC01A1RIPS'; 'ALC238Tires'; 'ALC01A1RIPS' ];
celldata = cellstr(data);
Total_Rows = [3;2;3;2];
% Duplicate the data based on the Total Rows values
for i = 1:length(Total_Rows)
Dup_Data = cell(Total_Rows(i), 1);
Dup_Data(:)= celldata(i);
output{i} = Dup_Data;
end
output2 = output';
This results in:
output2 =
4×1 cell array
{1×3 cell} where all 3 cells contain ALC238Tires
{1×2 cell} where both cells contain ALC01A1RIPS
{1×3 cell} where all 3 cells contain ALC238Tires
{1×2 cell} where both cells contain ALC01A1RIPS
But what I'd like is a 10 x 1 cell array of the following;
'ALC238Tires'
'ALC238Tires'
'ALC238Tires'
'ALC01A1RIPS'
'ALC01A1RIPS'
'ALC238Tires'
'ALC238Tires'
'ALC238Tires'
'ALC01A1RIPS'
'ALC01A1RIPS'
Can this be done?
Thank you.

採用された回答

Stephen23
Stephen23 2018 年 4 月 25 日
編集済み: Stephen23 2018 年 4 月 25 日
Using repelem is much simpler:
data = {'ALC238Tires'; 'ALC01A1RIPS'; 'ALC238Tires'; 'ALC01A1RIPS'};
rows = [3;2;3;2];
repelem(data,rows)
Or using your loop output:
output2 = [output{:}].';
  1 件のコメント
Brad
Brad 2018 年 4 月 25 日
I forgot all about the repelem function. Thanks Stephen!

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by