Info

この質問は閉じられています。 編集または回答するには再度開いてください。

sort a string based on particular value

4 ビュー (過去 30 日間)
Sajid Afaque
Sajid Afaque 2020 年 3 月 19 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello All,
i have a list of type
ch = { 'temp_25_vgs_8', 'temp_25_vgs_3', 'temp_25_vgs_9', 'temp_25_vgs_10', 'temp_20_vgs_8', 'temp_20_vgs_5', 'temp_25_vgs_5'};
i need to sort it based on vgs value (or both vgs and temp) and get back the list as
ch = { 'temp_20_vgs_5',
'temp_20_vgs_8',
'temp_25_vgs_3',
'temp_25_vgs_5',
'temp_25_vgs_8',
'temp_25_vgs_9',
'temp_25_vgs_10'
} ;
please help me resolve that,
than you in advance

回答 (1 件)

Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日
編集済み: Sriram Tadavarty 2020 年 3 月 19 日
Hi Sajid,
The sort function with the arrayfun can be used to check this.
chs = arrayfun(@(x) str2double(string([ch{x}(5+1:8-1) ch{x}(13:end)])),1:numel(ch))
[~,i] = sort(chs);
out = ch(i);
Hope this helps.
Regards,
Sriram
  2 件のコメント
Sajid Afaque
Sajid Afaque 2020 年 3 月 19 日
Hello Sriram,
it is only taking temp into consideration.
i need it to get sort based on vgs value also
K
K>> sort(ch)'
ans =
'temp_20_vgs_5'
'temp_20_vgs_8'
'temp_25_vgs_10'
'temp_25_vgs_3'
'temp_25_vgs_5'
'temp_25_vgs_8'
'temp_25_vgs_9'
Sriram Tadavarty
Sriram Tadavarty 2020 年 3 月 19 日
Hi Sajid,
Here is what you are looking for, if you know what the index locations are already, then you perform something as such:
chs = arrayfun(@(x) str2double(string([ch{x}(5+1:8-1) ch{x}(13:end)])),1:numel(ch))
[~,i] = sort(chs);
out = ch(i);
To find the locations exactly which has the starting numbers, you can use this
ind = regexp(ch,'_'); % it provides index in each cell
Let me know if this helped.
Thanking you.
Regards,
Sriram

この質問は閉じられています。

タグ

製品


リリース

R2013b

Community Treasure Hunt

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

Start Hunting!

Translated by