Manipulate data in cells
2 次查看(过去 30 天)
显示 更早的评论
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 255x1, 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 638x5, 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!
0 个评论
采纳的回答
Matt Kindig
2012-7-12
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!