Search code examples
combinationsoctavecell-array

how can I combine a cell array and two scalars to obtain a cell array of string, scalar1, scalar2 elements in Octave avoiding loops?


I have a cell array like P and two other float variables

P = {"GG+G[G]", "GG", "GG-GGG", "GG[][GG]", "[G[GG-G]]", "G[GG]+[G]"};
val1 = 0.01;
val2 = 0.3;

And I would like to build the following data structure without using a loop because the P cell array can contain a large number of elements:

Inputparam = 
{
    [1,1] = {
              [1,1] = "GG+G[G]"
              [1,2] = 0.01
              [1,3] = 0.3
            }
    [1,2] = {
              [1,1] = "GG"
              [1,2] = 0.01
              [1,3] = 0.3
            }
    [1,3] = {
              [1,1] = "GG-GGG"
              [1,2] = 0.01
              [1,3] = 0.3
            }
    [1,4] = {
              [1,1] = "GG[][GG]"
              [1,2] = 0.01
              [1,3] = 0.3
            }
    [1,5] = {
              [1,1] = "[G[GG-G]]"
              [1,2] = 0.01
              [1,3] = 0.3
            }
    [1,6] = {
              [1,1] = "G[GG]+[G]"
              [1,2] = 0.01
              [1,3] = 0.3
            }
}

I've tried several options but with most of them what I got was a concatenation and not a combination of the elements.

The purpose of this structure is to be the argument of parcellfun function that's why I need to have each element of P, with val1 and val2 values.

I'm also considering using an anonymum function instead of allocation all this data in memory. does it make sense?

thanks in advance.


Solution

  • I suggest that, instead of a cell array of cells arrays, you create a 2D cell array, just because this is much easier to generate and, for large arrays, it takes up less memory too:

    P = {"GG+G[G]", "GG", "GG-GGG", "GG[][GG]", "[G[GG-G]]", "G[GG]+[G]"};
    P(2,:) = 0.01;
    P(3,:) = 0.2;
    

    The cell array is indexed using P{1,5}, rather than P{5}{1} as the cell array in the OP.

    Another alternative is to use struct arrays:

    P = {"GG+G[G]", "GG", "GG-GGG", "GG[][GG]", "[G[GG-G]]", "G[GG]+[G]"};
    P = struct('name',P,'val1',0.01,'val2',0.2);
    

    The struct is indexed as P(5).name rather than P{5}{1} (and P(5).val1 instead of P{5}{2}, etc.).