Azzera filtri
Azzera filtri

How can i perform lexicographic sorting on a cell ?

4 visualizzazioni (ultimi 30 giorni)
Aparna
Aparna il 31 Lug 2014
Risposto: Arun Mathew Iype il 31 Lug 2014
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?

Risposte (1)

Arun Mathew Iype
Arun Mathew Iype il 31 Lug 2014
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]

Categorie

Scopri di più su Characters and Strings 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