フィルターのクリア

Automatically generate new matrices or arrays from unique values in matrix

3 ビュー (過去 30 日間)
Ryan Utz
Ryan Utz 2012 年 1 月 26 日
編集済み: the cyclist 2017 年 5 月 24 日
Hello,
I'm moderately experienced with programming, and I cannot believe how challenging I am finding this hurdle...
Suppose I have the following matrix:
a = [1,1;1,2.5;1,3;2,2;2,2;2,2;3,10;3,11;3,14;3,19;4,9]
and the first column represents a unique site or subject that I need to analyze separately. How can I make, in this case, 4 new matrices or arrays, within the workspace so that I have products that look like this:
site1 = [1,1;1,2.5;1,3]
site2 = [2,2;2,2;2,2]
My real data is more advanced than this, of course, but I cannot seem to figure out how to separate the data into separate sub-data sets based on a unique values. This seems to be a simple problem but I've tried looking at for-loops, the command 'unique', etc. and am getting absolutely nowhere.
Thanks ahead of time,
Ryan

採用された回答

Sean de Wolski
Sean de Wolski 2012 年 1 月 26 日
編集済み: the cyclist 2017 年 5 月 24 日
B = accumarray(a(:,1),a(:,2),[],@(x){x})
Using it directly like this, the index will be the leading number. Why you would need to replicate it, I don't know but it could be done easily. I.e:
B=
[3x1 double]
[3x1 double]
[4x1 double]
[ 4]
Thus the first 3x1 could get three ones because it's the first index, etc. Let me know if you need more clarification.
EDIT (from the cyclist): Be sure to see Stephen Cobeldick's comment to this answer, which solves this entirely.
  2 件のコメント
Stephen23
Stephen23 2017 年 5 月 24 日
編集済み: Stephen23 2017 年 5 月 24 日
It is also easy to use accumarray to get the entire rows, which is also simpler than the accepted answer:
>> a = [1,1;1,2.5;1,3;2,2;2,2;2,2;3,10;3,11;3,14;3,19;4,9];
>> C = accumarray(a(:,1),(1:size(a,1))',[],@(r){a(r,:)});
>> C{:}
ans =
1 1
1 2.5
1 3
ans =
2 2
2 2
2 2
ans =
3 10
3 11
3 14
3 19
ans =
4 9
the cyclist
the cyclist 2017 年 5 月 24 日
I've unaccepted my answer and accepted this one, which is indeed syntactically simpler. However, if one is having a difficult time understanding the underlying "magic" of accumarray, I would encourage you to look at my alternative solution, which exposes the algorithm more clearly.

サインインしてコメントする。

その他の回答 (1 件)

the cyclist
the cyclist 2012 年 1 月 26 日
There are many ways to slice this particular cat. Here is one way I typically use. I've tried to use descriptive variable names, to help you understand the logic of what I am doing.
a = [1,1;1,2.5;1,3;2,2;2,2;2,2;3,10;3,11;3,14;3,19;4,9];
[uniqueFirstColumnValues,indexToUnique,indexFromUniqueBackToAll] = unique(a(:,1));
numberUniqueValues = numel(uniqueFirstColumnValues);
subsetData = cell(numberUniqueValues,1);
for nu = 1:numberUniqueValues
indexToThisUniqueValue = (indexFromUniqueBackToAll==nu);
subsetData{nu} = a(indexToThisUniqueValue,:);
end
Note that each subset of data is a differently sized matrix, so I chose to store the results in a cell array.
  2 件のコメント
Ryan Utz
Ryan Utz 2012 年 1 月 26 日
Thanks! Seems to work...
Rachel Terry
Rachel Terry 2017 年 5 月 23 日
編集済み: Rachel Terry 2017 年 5 月 23 日
ahhh, just what I needed! Thanks from 5 years later!

サインインしてコメントする。

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by