フィルターのクリア

advanced sorting of cell array of strings

13 ビュー (過去 30 日間)
Kilian
Kilian 2012 年 7 月 28 日
編集済み: Eldhose Poulose 2020 年 3 月 6 日
I have the following array that I sorted in descending order (natural sort using sortn.m)
files = [
'a_20_15rpm_05.txt'
'a_20_14rpm_05.txt'
'a_20_12rpm_50.txt'
'a_20_12rpm_10.txt'
'a_20_12rpm_05.txt'
'a_20_5rpm_05.txt'
'a_20_4rpm_50.txt'
'a_20_4rpm_05.txt'
'a_20_2rpm_05.txt']
Here is what I need: the elements with equal rpms (index 3, 4 and 5 as well as 7 and 8) need to be sorted in ascending order. The final array should have the following form
files = [
'a_20_15rpm_05.txt'
'a_20_14rpm_05.txt'
'a_20_12rpm_05.txt'
'a_20_12rpm_10.txt'
'a_20_12rpm_50.txt'
'a_20_5rpm_05.txt'
'a_20_4rpm_05.txt'
'a_20_4rpm_50.txt'
'a_20_2rpm_05.txt']
I'd really appreciate some help on this. Thanks a lot for your answers in advance.
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 7 月 28 日
where are index 4 ,5 , 7 and 8?

サインインしてコメントする。

回答 (3 件)

Oleg Komarov
Oleg Komarov 2012 年 7 月 28 日
Your sample input:
files = {
'a_20_15rpm_05.txt'
'a_20_14rpm_05.txt'
'a_20_12rpm_50.txt'
'a_20_12rpm_10.txt'
'a_20_12rpm_05.txt'
'a_20_5rpm_05.txt'
'a_20_4rpm_50.txt'
'a_20_4rpm_05.txt'
'a_20_2rpm_05.txt'}
% Match the second and third group of digits (it would be nice to see more elegant ways to do that)
out = regexp(files, '(a_20_|rpm_|.txt)','split');
% Convert numeric strings to numbers and "unpack"
out = cell2mat(cellfun(@(x) str2double(x(:,2:3)),out,'un',0));
% Sort
[trash,idx] = sortrows(out,[-1,2]);
files(idx)

Andrei Bobrov
Andrei Bobrov 2012 年 7 月 28 日
k = regexp(files,'((?<=a_20_)\d*)|((?<=rpm_)\d*)','match');
[ii,ii] = sortrows(str2double(cat(1,k{:})),[-1 2]);
files(ii)
  1 件のコメント
Eldhose Poulose
Eldhose Poulose 2020 年 3 月 6 日
編集済み: Eldhose Poulose 2020 年 3 月 6 日
This Works for me. THANK YOU Andrei.
k= regexp(allNames,'((?<=out_)\d*)|((?<=_TRUE1_))','match'); % d* is for the varying variable, for example, 1 2 3 4 5 ...so on
[~,ii]= sortrows(str2double(cat(1,k{:}))); % [-1 2] is for ascending descending try with and without this
allNames= allNames(ii);

サインインしてコメントする。


Kilian
Kilian 2012 年 7 月 30 日
thanks a lot for all your help. this worked fine.
  1 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 7 月 30 日
Please accept the answer which you used to mark this thread closed.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by