How to extract labeled rows of a matrix?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
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
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일
편집: 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
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 =
[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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Cell Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!