2017-02-02 15 views
1

我有單元陣列:除法單元陣列分成組Matlab的

M = cell(5,3); 

M{1,1} = ['+' '-' '-'; '+' '+' '+'; '-' '-' '+']; 
M{2,1} = ['+' '-' '-'; '+' '+' '+'; '+' '+' '+']; 
M{3,1} = ['+' '-' '-'; '+' '+' '+'; '+' '+' '+']; 
M{4,1} = ['+' '-' '-'; '+' '+' '+'; '-' '-' '+']; 
M{5,1} = ['+' '-' '-'; '-' '+' '-'; '-' '-' '+']; 

M{1,2} = ['+' '0' '-'; '+' '+' '+'; '-' '-' '+']; 
M{2,2} = ['+' '0' '-'; '+' '+' '+'; '0' '+' '+']; 
M{3,2} = ['+' '0' '-'; '0' '+' '+'; '+' '+' '+']; 
M{4,2} = ['+' '-' '-'; '+' '+' '+'; '0' '-' '+']; 
M{5,2} = ['+' '-' '-'; '-' '0' '-'; '0' '-' '+']; 

M{1,3} = 1; 
M{2,3} = 3; 
M{3,3} = 7; 
M{4,3} = 25; 
M{5,3} = 33; 

我需要組的M所有行根據從第一列矩陣的平等。因此,有3個新的小電池陣列M1M2M3

M1{1,1} = M{1,1}; 
M1{2,1} = M{4,1}; 
M1{1,2} = M{1,2}; 
M1{2,2} = M{4,2}; 
M1{1,3} = M{1,3}; 
M1{2,3} = M{4,3}; 

M2{1,1} = M{2,1}; 
M2{2,1} = M{3,1}; 
M2{1,2} = M{2,2}; 
M2{2,2} = M{3,2}; 
M2{1,3} = M{2,3}; 
M2{2,3} = M{3,3}; 


M3{1,1} = M{5,1}; 
M3{1,2} = M{5,2}; 
M3{1,3} = M{5,3}; 

什麼是可能的方式做到這一點?

+2

你需要更長的描述你需要什麼和[mcve]。另外,請閱讀[問] –

+0

你可以顯示一些數據的例子:你有什麼數據,你想達到什麼目的? –

回答

0

你可以做到以下幾點:

% collect all different strings from the first column in M: 
str = cellfun(@(x) x(:).', M(:,1),'UniformOutput',false); 
% find all unique strings, and their indecies: 
[ustr,~,ic] = unique(str); 
% initialize new cell array: 
MM = cell(numel(ustr),1); 
% fill the new array by the corresponding rows in M for each unique string 
for k = 1:numel(ustr) 
    MM(k) = {M(ic==k,:)}; 
end 

這將導致在單元陣列MM

MM = 
    {2x3 cell} 
    {2x3 cell} 
    {1x3 cell} 

其中MM{k}是喜歡你的MK(例如MM(1)是你M1)。除了作爲解決此問題的更一般方法外,最好將所有合成單元數組打包到一個索引變量中,而不是在工作空間中包含許多變量。