Search code examples
arraysmatlabfor-loopcell-array

Accessing a cell array


I have a cell array A who's dimensions is in multiples of 8. For ex 1x192 , now i want to pick the elements of the cell array in the following way.

1,9,17,25...nth elements should be stored it in a separate cell array. similarly 2,10,18,26...n in a separate cell array and another set would be 3,11,19,27 in a separate cell array. this must be continued till 8,16,24,32..n

How can i automate this, where the cell array size is dynamic. But it is always a multiple of 8.

Any help would be appreciated thanks


Solution

  • The simplest thing would be reshape: since the cell array's length is always divisible by 8, you can reshape it to be 8 rows tall. Each row then is one of your separate cell arrays:

    >> c = num2cell(1:192);
    
    >> c8 = reshape(c(:), 8, [])
    c8 =
      8x24 cell array
      Columns 1 through 9
        {[1]}    {[ 9]}    {[17]}    {[25]}    {[33]}    {[41]}    {[49]}    {[57]}    {[65]}
        {[2]}    {[10]}    {[18]}    {[26]}    {[34]}    {[42]}    {[50]}    {[58]}    {[66]}
        {[3]}    {[11]}    {[19]}    {[27]}    {[35]}    {[43]}    {[51]}    {[59]}    {[67]}
        {[4]}    {[12]}    {[20]}    {[28]}    {[36]}    {[44]}    {[52]}    {[60]}    {[68]}
        {[5]}    {[13]}    {[21]}    {[29]}    {[37]}    {[45]}    {[53]}    {[61]}    {[69]}
        {[6]}    {[14]}    {[22]}    {[30]}    {[38]}    {[46]}    {[54]}    {[62]}    {[70]}
        {[7]}    {[15]}    {[23]}    {[31]}    {[39]}    {[47]}    {[55]}    {[63]}    {[71]}
        {[8]}    {[16]}    {[24]}    {[32]}    {[40]}    {[48]}    {[56]}    {[64]}    {[72]}
      Columns 10 through 18
        {[73]}    {[81]}    {[89]}    {[ 97]}    {[105]}    {[113]}    {[121]}    {[129]}    {[137]}
        {[74]}    {[82]}    {[90]}    {[ 98]}    {[106]}    {[114]}    {[122]}    {[130]}    {[138]}
        {[75]}    {[83]}    {[91]}    {[ 99]}    {[107]}    {[115]}    {[123]}    {[131]}    {[139]}
        {[76]}    {[84]}    {[92]}    {[100]}    {[108]}    {[116]}    {[124]}    {[132]}    {[140]}
        {[77]}    {[85]}    {[93]}    {[101]}    {[109]}    {[117]}    {[125]}    {[133]}    {[141]}
        {[78]}    {[86]}    {[94]}    {[102]}    {[110]}    {[118]}    {[126]}    {[134]}    {[142]}
        {[79]}    {[87]}    {[95]}    {[103]}    {[111]}    {[119]}    {[127]}    {[135]}    {[143]}
        {[80]}    {[88]}    {[96]}    {[104]}    {[112]}    {[120]}    {[128]}    {[136]}    {[144]}
      Columns 19 through 24
        {[145]}    {[153]}    {[161]}    {[169]}    {[177]}    {[185]}
        {[146]}    {[154]}    {[162]}    {[170]}    {[178]}    {[186]}
        {[147]}    {[155]}    {[163]}    {[171]}    {[179]}    {[187]}
        {[148]}    {[156]}    {[164]}    {[172]}    {[180]}    {[188]}
        {[149]}    {[157]}    {[165]}    {[173]}    {[181]}    {[189]}
        {[150]}    {[158]}    {[166]}    {[174]}    {[182]}    {[190]}
        {[151]}    {[159]}    {[167]}    {[175]}    {[183]}    {[191]}
        {[152]}    {[160]}    {[168]}    {[176]}    {[184]}    {[192]}
    
    >> c8(4,:)
    ans =
      1x24 cell array
      Columns 1 through 9
        {[4]}    {[12]}    {[20]}    {[28]}    {[36]}    {[44]}    {[52]}    {[60]}    {[68]}
      Columns 10 through 18
        {[76]}    {[84]}    {[92]}    {[100]}    {[108]}    {[116]}    {[124]}    {[132]}    {[140]}
      Columns 19 through 24
        {[148]}    {[156]}    {[164]}    {[172]}    {[180]}    {[188]}