How do I sort an array according to the first column?

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Image Analyst
Image Analyst il 28 Lug 2022
Modificato: Image Analyst il 28 Lug 2022
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 Commenti
Image Analyst
Image Analyst il 28 Lug 2022
Modificato: Image Analyst il 28 Lug 2022
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.
lil brain
lil brain il 28 Lug 2022
The join() function actually worked really well. So I used that as it was the simplest approach thanks!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by