Split a matrix according to sizes of cell arrays and store the result in cells/table

2 views (last 30 days)
Franz Lehner
Franz Lehner on 23 Apr 2021
Answered: Tarunbir Gambhir on 26 Apr 2021
Hey. I have a matrix A of the size 26813x2 which contains data (classifier posterior probabilities for IR spectra) I collected from 104 sample positions.
I have stored the original spectra of this set in cells in tbl.spectra in a table tbl, along with the other information. (see image)
What I need to do now is split this matrix A into submatrices/cells that have exactly the size of the rows in the cells in tbl.spectra, so that I can then add them to another column in the table to clearly identify to which measuring position/patient my data in A belongs.
I am pretty sure mat2cell is the function to use here. Unfortunately, the rowDist is not uniform, otherwise it would be easy. Usually, there are 256 spectra recorded per position, but some had to removed before classification.

Answers (1)

Tarunbir Gambhir
Tarunbir Gambhir on 26 Apr 2021
For this scenario, you can probably set a for loop to split the matrix "A" into cells with the same row dimensions as tbl.spectra. You can try out the following approach.
% random data setup
A = rand(256*4,2);
spectra = cell(4,1);
spectra{1} = rand(255,441);
spectra{2} = rand(257,441);
spectra{3} = rand(254,441);
spectra{4} = rand(258,441);
tbl = cell2table(spectra);
% Splitting of A into cells according to content in tbl.spectra
Asplit = cell(4,1);
offstart = 1;
for i =1:length(tbl.spectra)
Asplit{i} = A(offstart:size(tbl.spectra{i},1)+offstart-1,:);
offstart = offstart + size(tbl.spectra{i},1);
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by