How to create a cell array by repeating a row vector

I have a row vector A=[1:10]; I want to create cell array CA= cell(1,10) , where CA(1,1)= [A(1) A(1);A(1) A(1)], CA(1,2)=[A(2) A(2);A(2) A(2)]... so on and so forth. I understand that this oprtation can be done using a for loop. However, is there a way of doing this using cell array indexing, or some other faster method than a for loop ?

回答 (1 件)

KSSV
KSSV 2022 年 4 月 17 日

0 投票

A = 1:10 ;
B = reshape(repelem(A,4,1),2,2,10) ;
C = num2cell(B,[1 2]) ;
celldisp(C)
C{1,1,1} = 1 1 1 1 C{1,1,2} = 2 2 2 2 C{1,1,3} = 3 3 3 3 C{1,1,4} = 4 4 4 4 C{1,1,5} = 5 5 5 5 C{1,1,6} = 6 6 6 6 C{1,1,7} = 7 7 7 7 C{1,1,8} = 8 8 8 8 C{1,1,9} = 9 9 9 9 C{1,1,10} = 10 10 10 10

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

製品

リリース

R2021b

質問済み:

2022 年 4 月 17 日

回答済み:

2022 年 4 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by