How do I sort an array according to the first column?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
Hi,
I have a cell array which I would like to sort according to the first column. But when I try using
high_emp_sort = sort(high_empathy_ID_factors);
% or
high_emp_sort = sortrows(high_empathy_ID_factors);
... then I only get back errors. Could somebody kindly help?
Jeff
채택된 답변
Image Analyst
2022년 7월 28일
편집: Image Analyst
2022년 7월 28일
Did you mean by the first numerical column? Like this:
%====================================================================================
% Read in data.
storedStructure = load('high_empathy_ID_factors.mat');
ca = storedStructure.high_empathy_ID_factors;
data = cell2mat(ca(:, 2:end))
data = 17×4
18 12 14 16
17 19 15 11
8 18 14 22
25 11 17 7
16 22 9 5
11 13 14 10
13 23 16 19
10 18 17 25
9 14 14 15
15 18 17 10
fprintf('Read %d rows by %d columns into data.\n', size(data, 1), size(data, 2));
Read 17 rows by 4 columns into data.
%====================================================================================
% Sort by column 1.
fprintf('Sorting data by column 1.\n');
Sorting data by column 1.
[sortedData, sortOrder] = sortrows(data, 1);
%====================================================================================
% Sort original cell array also.
fprintf('Sorting original cell array in the same order of rows.\n');
Sorting original cell array in the same order of rows.
ca = ca(sortOrder, :)
ca = 17×5 cell array
{["61728b3e4e680d6c607f514c"]} {[ 8]} {[18]} {[14]} {[22]}
{["5d4a41890e604c00011ade8b"]} {[ 9]} {[14]} {[14]} {[15]}
{["5dc4bc30569be3387eb6b52f"]} {[10]} {[18]} {[17]} {[25]}
{["5f5e7de4c81d3672642cd612"]} {[10]} {[25]} {[15]} {[21]}
{["6134c80f7eab0971588b3d3a"]} {[11]} {[13]} {[14]} {[10]}
{["5fb6ed2116919c000a99249e"]} {[13]} {[23]} {[16]} {[19]}
{["5b59a51fca6d01000157a8c3"]} {[14]} {[ 2]} {[17]} {[11]}
{["611b1d3794ac948af7e57e7a"]} {[15]} {[18]} {[17]} {[10]}
{["5fd7782dee03dc08d3f3f491"]} {[15]} {[18]} {[16]} {[ 1]}
{["5ecbb2347a5125043b1d3588"]} {[15]} {[26]} {[17]} {[13]}
{["6155c449ebffeae654abd854"]} {[16]} {[22]} {[ 9]} {[ 5]}
{["5eac84ff4efbe80ffd3962f1"]} {[16]} {[13]} {[18]} {[ 6]}
{["5f5503435d41a489068ff50b"]} {[17]} {[19]} {[15]} {[11]}
{["61520b079436973e05f72d33"]} {[18]} {[12]} {[14]} {[16]}
{["5e92f01a49a5ba62bd0d5693"]} {[18]} {[15]} {[14]} {[ 7]}
{["607ed750ad800928b1861317"]} {[19]} {[18]} {[14]} {[15]}
Or did you really mean by the long alphanumeric hexadecimal strings in the first column? Like this:
%====================================================================================
% Read in data.
storedStructure = load('high_empathy_ID_factors.mat')
storedStructure = struct with fields:
high_empathy_ID_factors: {17×5 cell}
ca = storedStructure.high_empathy_ID_factors;
data = cell2mat(ca(:, 2:end))
data = 17×4
18 12 14 16
17 19 15 11
8 18 14 22
25 11 17 7
16 22 9 5
11 13 14 10
13 23 16 19
10 18 17 25
9 14 14 15
15 18 17 10
[rows, columns] = size(data);
fprintf('Read %d rows by %d columns into data.\n', rows, columns);
Read 17 rows by 4 columns into data.
%====================================================================================
% Convert hex numbers in column 1 to decimal
for row = 1 : rows
c = char(ca{row, 1});
% Can only convert the first 16 digits.
numbers(row) = hex2dec(c(1:16));
end
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
%====================================================================================
% Sort by column 1.
fprintf('Sorting data by column 1.\n');
Sorting data by column 1.
[sortedData, sortOrder] = sort(numbers)
sortedData = 1×17
1.0e+18 *
6.5825 6.7223 6.7567 6.8148 6.8220 6.8307 6.8694 6.8721 6.8970 6.9061 6.9532 6.9972 7.0044 7.0118 7.0127 7.0137 7.0218
sortOrder = 1×17
12 9 8 14 13 16 2 15 7 11 17 10 6 4 1 5 3
%====================================================================================
% Sort original cell array also.
fprintf('Sorting original cell array in the same order of rows.\n');
Sorting original cell array in the same order of rows.
ca = ca(sortOrder, :)
ca = 17×5 cell array
{["5b59a51fca6d01000157a8c3"]} {[14]} {[ 2]} {[17]} {[11]}
{["5d4a41890e604c00011ade8b"]} {[ 9]} {[14]} {[14]} {[15]}
{["5dc4bc30569be3387eb6b52f"]} {[10]} {[18]} {[17]} {[25]}
{["5e92f01a49a5ba62bd0d5693"]} {[18]} {[15]} {[14]} {[ 7]}
{["5eac84ff4efbe80ffd3962f1"]} {[16]} {[13]} {[18]} {[ 6]}
{["5ecbb2347a5125043b1d3588"]} {[15]} {[26]} {[17]} {[13]}
{["5f5503435d41a489068ff50b"]} {[17]} {[19]} {[15]} {[11]}
{["5f5e7de4c81d3672642cd612"]} {[10]} {[25]} {[15]} {[21]}
{["5fb6ed2116919c000a99249e"]} {[13]} {[23]} {[16]} {[19]}
{["5fd7782dee03dc08d3f3f491"]} {[15]} {[18]} {[16]} {[ 1]}
{["607ed750ad800928b1861317"]} {[19]} {[18]} {[14]} {[15]}
{["611b1d3794ac948af7e57e7a"]} {[15]} {[18]} {[17]} {[10]}
{["6134c80f7eab0971588b3d3a"]} {[11]} {[13]} {[14]} {[10]}
{["614ec4f7be36eb900905644c"]} {[25]} {[11]} {[17]} {[ 7]}
{["61520b079436973e05f72d33"]} {[18]} {[12]} {[14]} {[16]}
{["6155c449ebffeae654abd854"]} {[16]} {[22]} {[ 9]} {[ 5]}
댓글 수: 3
Sorry if I was unclear. I actually did mean the first column (long alphanumeric hexadecimal strings). I have two more arrays ("high_loc_ID_factors", "high_itc_sopi_ID_factors") which have a first column with matching strings (participant IDs).
My idea is to sort both of these arrays so that I can concatenate them and have the rows match each other (line up the rows according to the participant IDs.
Does that make sense?
Image Analyst
2022년 7월 28일
편집: Image Analyst
2022년 7월 28일
See my edited answer above.
Or maybe read them into tables with readtable(), and use one of the members of the "join" functions to combine the tables.
The join() function actually worked really well. So I used that as it was the simplest approach thanks!
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
