I am currently writing a function that converts a cell array into sparse block matrix.
I did achieve my purpose and now I am trying to optimize the code, but I just cannot seem to find a way to speed up the vectorization of matrices within the cell.
Currently, I have set the input as a column cell vector like the following:
Input = cell(Blocks,1);
Each cell inside the input has variously sized matrices.
To vectorize the matrices inside the cell, I am currently using this code:
V = cellfun(@(x) x(:), Input, 'un', 0);
But my profiler keeps on telling me that this part of my code takes up a lot of time.
I did try to change this into a for loop:
Blks = size(Input,1);
V = cell(Blks,1);
for i = 1:Blks
V{i} = Input{i}(:);
end
However, this only makes my code run slower than my previous code.
Is there another alternative way to speed up the vectorization process of the matrices within the cell?
Thank you very much in advance!
I wanted to add another information.
After I calculate V, I concatenate all the cells and turn them into a single vector Val by doing so:
Val = cat(1,V{:});
At first I tried using cell2mat instead of cat, but it turns out that cat is much faster.
If there is a way to calculate Val without going through calculating V, it would be much better :)
Thanks again!