How to select specific values from a matrix?

7 ビュー (過去 30 日間)
Vijoya
Vijoya 2014 年 12 月 22 日
編集済み: Azzi Abdelmalek 2014 年 12 月 22 日
I have a matrix which looks like this -
val =
[
1, a, b, c;
1, c, d, e;
1, a, b, d;
2, e, d, c;
2, r, t, v;
3, e, t, z;
3, r, t, x;
3, d, e, v;
3, r, d, f;
4, f, d, x;
1, r, t, c;
2, r, x, t;
4, a, d, x;
]
it may have varying numbers of rows. The first column contains the identifier that I want to use to sort the data such that all the columns with the index 1, or 2, or 3, or 4 are in one separate matrix. So for the example above, I should be able to create 4 matrix with the rows for the values from the three columns in that specific index.
So the output will be -
val_index1 =
[
1, a, b, c;
1, c, d, e;
1, a, b, d;
1, r, t, c;
]
val_index2 =
[
2, e, d, c;
2, r, t, v;
2, r, x, t;
]
val_index3 =
[
3, e, t, z;
3, r, t, x;
3, d, e, v;
3, r, d, f;
]
val_index4 =
[
4, f, d, x;
4, a, d, x;
]
How can I do this?
many thanks for your suggestion, Vijoya

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 12 月 22 日
編集済み: Azzi Abdelmalek 2014 年 12 月 22 日
%Example
a=[1;1;1;2;2;3;3;3;3;4;1;2;4]
b=randi(9,numel(a),3)
c=[a b];
%------------------The code----------------
y=accumarray(a,1:numel(a),[],@(x) {c(x,:)})
celldisp(y)
You don't need to create a variable for each sub-matrix. You can get the four matrices:
y{1}
y{2}
y{3}
y{4}

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by