MATLAB: Manipulate data in cells

cellcell arraysmatrix manipulationsort

I have part of a program that has sorted an nx5 matrix by its fifth column, which contains integers from 1 to 255. The cell "bins" is 255×1, and each of its entries contains the 5-column rows of the original matrix, identified by the entry in the fifth column (ie. the first entry in the cell is 638×5, and for all of its rows, the fifth column contain the number 1). I would like to delete the fifth column from each entry in the bin, and then break each row of four numbers into four separate groups of three (columns 1, 2, 3; columns 1, 2, 4; columns 1, 3, 4; columns 2, 3 4). I am not familiar with the syntax for cell manipulation. Any help with this is appreciated. Thank you!

Best Answer

Hi Rebecca,
I think it would be easiest just to loop through all of the elements of the matrix. To access a given element in the cell array 'bins', use the curly braces {}, in a way that you similarly would use parentheses () for matrices.
For the first part:
output = cell(size(bins));
for k=1:length(bins),
mat = bins{k}; %get element in cell

mat(:,5)=[]; %remove 5th column

output{k} = mat; %assign to output cell array
end
The second part of your request is unclear. Once you break each row into four separate groups of three columns, what do you want to do with the data? Are you assigning each group as an element of the cell array (such that the final cell array is 255x4)? If so, this revised code will work:
output = cell(length(bins), 4);
for k=1:length(bins),
mat = bins{k}; %get element in cell
mat(:,5)=[]; %remove 5th column
output{k,1} = mat(:,[1 2 3]); %break columns into groups and
output{k,2} = mat(:,[1 2 4]); % assign to cell array
output{k,3) = mat(:,[1 3 4]);
output{k,4} = mat(:,[2 3 4]);
end