sorting string based on the value in it

28 ビュー (過去 30 日間)
Sajid Afaque
Sajid Afaque 2020 年 1 月 8 日
編集済み: Walter Roberson 2024 年 8 月 6 日
'temp_25_vgs_4',
'temp_25_vgs_5' ,
'temp_25_vgs_8' ,
'temp_25_vgs_6' ,
'temp_25_vgs_9',
'temp_25_vgs_7'
how do i sort the above in the increasing order of vgs.
how to sort the above kind of strings..i need it in order (
'temp_25_vgs_4'
'temp_25_vgs_5'
'temp_25_vgs_6'
'temp_25_vgs_7'
'temp_25_vgs_8'
'temp_25_vgs_9'
)
  4 件のコメント
Sajid Afaque
Sajid Afaque 2020 年 1 月 8 日
is their anyway where we can do without using these functions
Stephen23
Stephen23 2020 年 1 月 8 日
"is their anyway where we can do without using these functions"
Of course: parse the string to split/extract all relevant characters and numbers (e.g. based on a regular expression or isstrprop), convert the numbers to numeric, sort. You can search this forum to find examples of this, the search field is at the top of the page.

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

回答 (1 件)

Arjun
Arjun 2024 年 8 月 6 日
Hi,
As per my understanding of the question you want to custom sort a list of strings without using inbuilt functions. I can give you a basic workflow of how to proceed and some dummy code which you can use to construct solution for your application.
Steps:
  1. Preprocess the string.
  2. Manually extract the numeric part from the string after the last _(underscore).
  3. Construct the numeric value.
  4. Write custom sort function such as selection sort or bubble sort to sort the list of strings based on the numeric value extracted.
Here is some dummy code which might be helpful in better understanding of the solution.
% Define the list of variable names
variable_names = {
'temp_25_vgs_44',
'temp_25_vgs_5',
'temp_25_vgs_812',
'temp_25_vgs_6',
'temp_25_vgs_99',
'temp_25_vgs_7'
};
% Function to extract numeric part
function num = extract_vgs_value(str)
% Find the position of the last underscore
underscore_pos = find(str == '_', 1, 'last');
% Extract the numeric part after the last underscore
num_str = str(underscore_pos+1:end);
% Convert to number
num = 0;
for i = 1:length(num_str)
num = num * 10 + (num_str(i) - '0');
end
end
% Implement bubble sort to sort the variable names based on numeric values
n = length(variable_names);
for i = 1:n-1
for j = 1:n-i
if extract_vgs_value(variable_names{j}) > extract_vgs_value(variable_names{j+1})
% Swap the variable names
temp = variable_names{j};
variable_names{j} = variable_names{j+1};
variable_names{j+1} = temp;
end
end
end
% Display the sorted list of variable names
disp('Sorted list of variable names:');
disp(variable_names);
In the code provided above we have used find function from MATLAB whose documentation you can refer in the attached link below.
  1 件のコメント
Stephen23
Stephen23 2024 年 8 月 6 日
編集済み: Stephen23 2024 年 8 月 6 日
"Implement bubble sort to sort the variable names based on numeric values"
MATLAB already has a very efficient inbuilt SORT function.
Simpler and more efficient:
C = {'temp_25_vgs_44';'temp_25_vgs_5';'temp_25_vgs_812';'temp_25_vgs_6';'temp_25_vgs_99';'temp_25_vgs_7'}
C = 6x1 cell array
{'temp_25_vgs_44' } {'temp_25_vgs_5' } {'temp_25_vgs_812'} {'temp_25_vgs_6' } {'temp_25_vgs_99' } {'temp_25_vgs_7' }
[~,X] = sort(str2double(extract(C,digitsPattern+textBoundary)));
D = C(X)
D = 6x1 cell array
{'temp_25_vgs_5' } {'temp_25_vgs_6' } {'temp_25_vgs_7' } {'temp_25_vgs_44' } {'temp_25_vgs_99' } {'temp_25_vgs_812'}

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

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by