How to create a cell array identical in size, but containing cell position in each cell?

1 回表示 (過去 30 日間)
NeuronDB
NeuronDB 2021 年 12 月 1 日
回答済み: G A 2021 年 12 月 1 日
I have a cell array, with different number of elements in each cell. I convert the cells to a 1xn matrix with
For example,
A = {[1,2,3,4]; [1,2,5]; [5,6,1,2,4]; [44]}
A = 4×1 cell array
{[ 1 2 3 4]} {[ 1 2 5]} {[5 6 1 2 4]} {[ 44]}
I want to create a new cell array which is identical in size to A containing cell number as element sof each of the cell such that the output would look like -
B = {[1,1,1,1,]; [2,2,2]; [3,3,3,3,3]; [4]}
B = 4×1 cell array
{[ 1 1 1 1]} {[ 2 2 2]} {[3 3 3 3 3]} {[ 4]}
How can I make this?

採用された回答

G A
G A 2021 年 12 月 1 日
A = {[1,2,3,4]; [1,2,5]; [5,6,1,2,4]; [44]}
B = cell(size(A));
for k = 1:length(A)
B{k} = repmat(k,1,length(A{k}));
end

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by