How can i perform lexicographic sorting on a cell ?

조회 수: 4 (최근 30일)
Aparna
Aparna 2014년 7월 31일
답변: Arun Mathew Iype 2014년 7월 31일
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에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by