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
采纳的回答
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?
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)
