How to sort a part of a given array?

조회 수: 1 (최근 30일)
Lukas Hablowetz
Lukas Hablowetz 2022년 12월 7일
편집: Jonas 2022년 12월 12일
Hello,
I´m looking for a way to sort only a part of a given numeric array in Simulink. Lets say I´ve got an array with 20 elements [20 ... 1]. Depending on the application I just want to sort the first n [5] entries [20 19 18 17 16] in ascending order, so after the sorting the result of the entire array should look like [16 17 18 19 20 15 14 13...1].
Is there a Simulink block which already has this feature? I´m used to the sort block of the DSP toolbox but this only deals with the entire array at once.
Restrictions:
- Solution must be portable to Simulink
- the number n varies during the run-time of my application. It´s a paramter that can be adjust within 1 and 20
- the applicatoin will be converted to a C-Application by the embedded coder so there are also the given C restrictions of fixed array sizes
If there is no block I´d be glad to get any advice, how I could solve this efficiently.

답변 (2개)

Karim
Karim 2022년 12월 7일
Hi, below you can find some example code to demonstrate how to sort the first 'nSort' elements of a given vector.
% create a vector with 20 random integers
MyVec = randi(100,1,20)
MyVec = 1×20
30 30 52 14 12 19 40 15 31 100 60 72 33 16 28 1 55 5 72 39
% define the number of elements we want to sort
nSort = 5;
% sort the first 'nSort' elements:
MyVec(1:nSort) = sort(MyVec(1:nSort),'ascend')
MyVec = 1×20
12 14 30 30 52 19 40 15 31 100 60 72 33 16 28 1 55 5 72 39
  댓글 수: 1
Lukas Hablowetz
Lukas Hablowetz 2022년 12월 12일
Unfortunately not, I added some restrictions to the inital description.
But I´ll try to catch up your idea.

댓글을 달려면 로그인하십시오.


Jonas
Jonas 2022년 12월 7일
just use indexing to reassign those numbers
data=20:-1:1
data = 1×20
20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
data(1:5)=sort(data(1:5),'ascend')
data = 1×20
16 17 18 19 20 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
  댓글 수: 2
Lukas Hablowetz
Lukas Hablowetz 2022년 12월 12일
Unfortunately not, I added some restrictions to the inital description.
But I´ll try to catch up your idea.
Jonas
Jonas 2022년 12월 12일
편집: Jonas 2022년 12월 12일
without beeing familiar with Simulink, there may be a method to split the vector into two parts, sort the first part and combine them again?
this could be suitable?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Array and Matrix Mathematics에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by