# How to extract labeled rows of a matrix?

12 次查看（过去 30 天）
hello_world 2016-7-2

Hello Friends,
I have a matrix X of size NxD, where the Dth column is labeled. For example:
X = [1, 2, 3, Sunday
4, 5, 6, Monday
7, 8, 9, Sunday
10, 11, 12, Tuesday
13, 14, 15, Monday];
I want to extract the rows corresponding to each labels. So for above matrix, I should get the following matrices:
A = [ 1, 2, 3, Sunday
7, 8, 9, Sunday];
B = [4, 5, 6, Monday
13, 14, 15, Monday];
C = [10, 11, 12, Tuesday];
Here matrix A is of size 2xD, B is of size 2xD and C is of size 1xD.
##### 1 个评论显示 -1更早的评论隐藏 -1更早的评论
David Miller 2016-7-2
A = zeros(1,D);
B = zeros(1,D);
C = zeros(1,D);
a, b, c = 1;
for i = 1:N
if X(i,4) == Sunday
A(a,:) = X(i,:);
a = a + 1;
elseif X(i,4) == Monday
B(b,:) = X(i,:);
b = b + 1;
else
C(c,:) = X(i,:);
c = c + 1;
end
end

### 采纳的回答

Star Strider 2016-7-2
See if this does what you want:
X = {1, 2, 3, 'Sunday'
4, 5, 6, 'Monday'
7, 8, 9, 'Sunday'
10, 11, 12, 'Tuesday'
13, 14, 15, 'Monday'};
[UX,ia,ic] = unique(X(:,end), 'stable');
for k1 = 1:size(UX,1)
Out{k1} = X(k1==ic,:);
end
Out{1}
Out{2}
Out{3}
ans =
[1.0000e+000] [2.0000e+000] [3.0000e+000] 'Sunday'
[7.0000e+000] [8.0000e+000] [9.0000e+000] 'Sunday'
ans =
[ 4.0000e+000] [ 5.0000e+000] [ 6.0000e+000] 'Monday'
[13.0000e+000] [14.0000e+000] [15.0000e+000] 'Monday'
ans =
[10.0000e+000] [11.0000e+000] [12.0000e+000] 'Tuesday'
##### 1 个评论显示 -1更早的评论隐藏 -1更早的评论
Star Strider 2016-7-2
You can’t have a double array if you also have string variables. The only way you could use ‘Sunday’, ‘Monday’, ‘Tuesday’, &c. in a double array is if you assigned unique values to them as double variables, just as you would any other variable.
The code to do what you want would otherwise be essentially unchanged from that posted here, and would produce a double array but with the assigned numbers in the last column instead of the variable names in the original matrix.

### 更多回答（2 个）

Andrei Bobrov 2016-7-2

a = {1, 2, 3, 'Sunday'
4, 5, 6, 'Monday'
7, 8, 9, 'Sunday'
10, 11, 12, 'Tuesday'
13, 14, 15, 'Monday'};
[a1,~,cc] = unique(a(:,end),'stable');
out = accumarray(cc,1:size(a,1),[],@(x){a(x,:)});
##### 0 个评论显示 -2更早的评论隐藏 -2更早的评论

Image Analyst 2016-7-2
Here's another way to get A, B, and C, both mixed numbers and labels as a cell array, and with the numbers only extracted into a double array:
X = {1, 2, 3, 'Sunday'
4, 5, 6, 'Monday'
7, 8, 9, 'Sunday'
10, 11, 12, 'Tuesday'
13, 14, 15, 'Monday'}
% Get A
rowIdexes = ismember(X(:, 4), 'Sunday');
A = X(rowIdexes, :)
ANumbersOnly = cell2mat(A(:, 1:3))
% Get B
rowIdexes = ismember(X(:, 4), 'Monday');
B = X(rowIdexes, :)
BNumbersOnly = cell2mat(B(:, 1:3))
% Get C
rowIdexes = ismember(X(:, 4), 'Tuesday');
C = X(rowIdexes, :)
CNumbersOnly = cell2mat(C(:, 1:3))
Results:
A =
   'Sunday'
   'Sunday'
ANumbersOnly =
1 2 3
7 8 9
B =
[ 4] [ 5] [ 6] 'Monday'
   'Monday'
BNumbersOnly =
4 5 6
13 14 15
C =
   'Tuesday'
CNumbersOnly =
10 11 12
Please read the FAQ http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F since I'm not sure you understand numerical arrays vs. cell arrays.

### 类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

### Community Treasure Hunt

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

Start Hunting!