Automatically generate new matrices or arrays from unique values in matrix

조회 수: 4 (최근 30일)
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
Rachel Terry
Rachel Terry 2017년 5월 23일
편집: Rachel Terry 2017년 5월 23일
ahhh, just what I needed! Thanks from 5 years later!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by