sorting string based on the value in it
28 ビュー (過去 30 日間)
古いコメントを表示
'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 件のコメント
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
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:
- Preprocess the string.
- Manually extract the numeric part from the string after the last _(underscore).
- Construct the numeric value.
- 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
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'}
[~,X] = sort(str2double(extract(C,digitsPattern+textBoundary)));
D = C(X)
参考
カテゴリ
Help Center および File Exchange で Shifting and Sorting Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!