Create n matrix from one matrix according to elements of a column?
2 ビュー (過去 30 日間)
古いコメントを表示
Sleh Eddine Brika
2016 年 4 月 15 日
コメント済み: Sleh Eddine Brika
2016 年 4 月 16 日
I have a sorted matrix (according to the element of the third column)
A = [ 4 2 0 ;3 7 0 ; 8 9 3 ; 7 5 3; 4 4 3]
I want to get n matrix (in this case 2) from A, according to the value of the element of the third column, to get
M1 = [ 4 2 0 ; 3 7 0]
M2 = [8 9 3 ; 7 5 3; 4 4 3].
I know how many matrix I will have and the different elements to study using the function 'unique' that provide me a matrix (for this case) B=[0;3].
I tried with the functions 'genvarname' and 'eval' but I didn't find a good solution.
0 件のコメント
採用された回答
Andrei Bobrov
2016 年 4 月 15 日
編集済み: Andrei Bobrov
2016 年 4 月 15 日
[~,~,c] = unique(A(:,end));
M = accumarray(c,(1:size(A,1))',[],@(x){A(x,:)});
2 件のコメント
その他の回答 (1 件)
Azzi Abdelmalek
2016 年 4 月 15 日
A = [ 4 2 0 ;3 7 0 ; 8 9 3 ; 7 5 3; 4 4 3]
[ii,jj,kk]=unique(A(:,3))
out=accumarray(kk,1,[],@(x) {A(x,:)})
% the first matrix and the second are
out{1}
out{2}
It's not a good idea to generate multiple names http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
3 件のコメント
Azzi Abdelmalek
2016 年 4 月 15 日
@Sleh Eddine, it's easier to call your matrices with one variable and indices: out{1}, out{2},... then using multiple variables!
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!