MATLAB Answers

How to extract labeled rows of a matrix?

5 views (last 30 days)
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.
I will appreciate any advise!
  1 Comment
David Miller
David Miller on 2 Jul 2016
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

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 2 Jul 2016
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 Comment
Star Strider
Star Strider on 2 Jul 2016
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.

Sign in to comment.

More Answers (2)

Andrei Bobrov
Andrei Bobrov on 2 Jul 2016
Edited: Andrei Bobrov on 2 Jul 2016
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,:)});

Image Analyst
Image Analyst on 2 Jul 2016
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 =
[1] [2] [3] 'Sunday'
[7] [8] [9] 'Sunday'
ANumbersOnly =
1 2 3
7 8 9
B =
[ 4] [ 5] [ 6] 'Monday'
[13] [14] [15] 'Monday'
BNumbersOnly =
4 5 6
13 14 15
C =
[10] [11] [12] '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.

Community Treasure Hunt

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

Start Hunting!