0
votes

I have a 128 X 8192 matrix. I have split it into cell arrays of size 1X64 each using :

C = mat2cell(input_matrix, repmat(1,[1 128]), repmat(64,[1 128]));

Now I need to pass each of these cell arrays to an function say 'fn_X'. This function returns an array of varying sizes. I need the output for each of the cell arrays passed to be stored in a result matrix. I used the following code :

for(irl = 1 : 128)
        for(jrl = 1 : 128)
            Output_matrix(irl,jrl) = fn_X(C{irl,jrl});
         end
end

This gave me an error 'Subscripted assignment dimension mismatch.'

I then tried it in a column matrix using the following code :

king = 1;
    for(irl = 1 : 128)
        for(jrl = 1 : 128)
            Output_matrix(king) = fn_X(C{irl,jrl});
            king = king + 1;
        end
    end

This gave me an error 'In an assignment A(I) = B, the number of elements in B and I must be the same.'

I also tried using cell array assignment using the following code :

king = 1;
for(irl = 1 : 128)
    for(jrl = 1 : 128)
        Output_matrix(king) = fn_X(C{irl,jrl});
        king = king + 1;
    end
end

This gave an error 'Cell contents assignment to a non-cell array object.'

Any suggestions on how I can handle this?

1

1 Answers

0
votes

The problem on your code is that should use curly brackets on the left side of the assignment:

king = 1;
for irl = 1 : 128
    for jrl = 1 : 128
        Output_matrix{king} = fn_X(C{irl,jrl});
        king = king + 1;
    end
end

But this likely not the right way to do this. This is not the "style" of the language, the way that makes sense is to vectorize which will probably require fn_X handle this itself.

Alternatively, you can also do it with the much simpler (and often faster):

Output_matrix = cellfun (@fn_X, C, "UniformOutput", false);