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!