Vectorized way to assign columns/rows of matrix to struct array's field
2 views (last 30 days)
Currently I'm using a for loop to assign parts of a matrix to struct's field, like this:
len = 90000;
width = 51;
structArray(1:len) = struct("field",zeros(width,1));
matrixToAssign = rand(width,len);
structArray(i).field = matrixToAssign(:,i);
Because I could vectorize the calculations(not seen here), the assignment of the solutions in the for loop takes an equal amount of time relative to the calculation itself. Is there a way to do the assignment also in a fast vectorized way ?
The calculations I'm doing in this part of the script are depending on len. Typically for examples len>>90000 the accumulated calculations need around 3s and the accumulated assignments need around 2s (calculation and assignment are done multiple times). I have seen something like below, which tends in the right direction but isn't quite what I want to do. I have also seen transformations into cell arrays but I believe those are not faster than the loop method, but maybe I'm wrong.
[structArray.field] = deal(matrixToAssign);