How can i perform lexicographic sorting on a cell ?

12 次查看(过去 30 天)
Hi,
As a part of may work i first divide an image into number of overlapping blocks and dct for each blocks are calculated.Then from each block 4 features are extracted and they are saved into a cell.Then that cell contains 4 columns and rows equal to number ob blocks.Then i want to perform lexicographic sorting on this feature vector cell,how can i perform that?

回答(1 个)

Arun Mathew Iype
Arun Mathew Iype 2014-7-31
Check the sortrows function for sorting cell arrays which gives a lexicographical/dictionary sort. You can find examples and description in the below link.
Example :
%Create a 6-by-2 cell array of strings.
A = {'Germany' 'Lukas'; 'USA' 'William'; 'USA' 'Andrew'; ...
'Germany' 'Andreas'; 'USA' 'Olivia'; 'Germany' 'Julia'}
%Sort the rows of A.
B = sortrows(A)
Output:
A =
'Germany' 'Lukas'
'USA' 'William'
'USA' 'Andrew'
'Germany' 'Andreas'
'USA' 'Olivia'
'Germany' 'Julia'
B =
'Germany' 'Andreas'
'Germany' 'Julia'
'Germany' 'Lukas'
'USA' 'Andrew'
'USA' 'Olivia'
'USA' 'William'
In case you want to do a lexicographic sorting of numbers, you can use this workaround wherein you first change the numbers to string Example :
orig_cell_array ={7 4; 6 1; 20 2; 5 3}
% Convert to a string array
str_array = cellfun(@num2str,orig_cell_array,'UniformOutput',false)
% Do lexicographical sorting
sort_str_array = sortrows(str_array)
% Convert back to numbers
new_cell_array = cellfun(@str2num, sort_str_array,'UniformOutput',false)
Output :
orig_cell_array =
[ 7] [4]
[ 6] [1]
[20] [2]
[ 5] [3]
str_array =
'7' '4'
'6' '1'
'20' '2'
'5' '3'
sort_str_array =
'20' '2'
'5' '3'
'6' '1'
'7' '4'
new_cell_array =
[20] [2]
[ 5] [3]
[ 6] [1]
[ 7] [4]

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by