Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Creating an array by repeating the index of a cell by the length of the cell

1 回表示 (過去 30 日間)
Ryan Waite
Ryan Waite 2017 年 4 月 27 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
I have a cell array of data shown below. I would like to create a new array (output) where the index of the cell is repeated "x" times. Where "x" is equal to the length of that specific cell. I can do this with for loops, but can it be done with a simple function?
data = {[1,2,3], [4,5], [6], [7,8,9,10]}
% output = [1,1,1,2,2,3,4,4,4,4]

回答 (1 件)

John Malik
John Malik 2017 年 4 月 28 日
repelem(1:length(data), cellfun(@length, data))

製品

Community Treasure Hunt

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

Start Hunting!

Translated by