Sort array based on struct fields' values

22 visualizaciones (últimos 30 días)
slow_hand
slow_hand el 26 de Jun. de 2023
Comentada: slow_hand el 30 de Mzo. de 2024 a las 11:55
Hi all,
I'm having trubles with a specific operation: I'm trying to sort an array that contains four different numbers using a specific struct that associates to each of these number a specific value. For example I have that:
  • emitters = [12, 24, 48, 84];
  • Frequency.(strcat('Em_',num2str(emitters(idx_em)))) = frequency values associated to that specific emitter
I would like to sort the emitters array (row vector) in an increasing order based on the values indicated in the corresponding struct. So ordering the values in the struct in an increasing order will give an indication of how the array should be organized, and then order the latter as well.
How it can be done?
Thank you for the precious help!
  1 comentario
Stephen23
Stephen23 el 26 de Jun. de 2023
Editada: Stephen23 el 26 de Jun. de 2023
"How it can be done?"
SORT the vales from the structure, getting the 2nd output (the sort indices). Use the sort indices to rearrange the vector.
What have you tried so far?
Note that hiding meta-data in fieldnames is complicated and likely makes this task harder. Using e.g. a non-scalar structure instead would likely make this task easier and more efficient.

Iniciar sesión para comentar.

Respuesta aceptada

Rahul
Rahul el 26 de Jun. de 2023
According to the information shared, I believe you want to sort the array with respect to the Frequncy attributes associated with each element of emitters array.
Once the frequencies of all the elements in the emitters array is set according to this code:
Frequency.(strcat('Em_',num2str(emitters(idx_em)))) = frequency values associated to that specific emitter
Now by creating a cell array for emitter-frequency pairs,
pairs = cell(length(emitters), 2);
for i = 1:length(emitters)
emitter = emitters(i);
pairs{i, 1} = emitter;
pairs{i, 2} = Frequency.(strcat('Em_', num2str(emitter)));
end
Now, the pairs can be sorted based on frequncy values like,
sortedPairs = sortrows(pairs, 2);
We can extract the array of sorted emitters from the pairs like,
sortedEmitters = cell2mat(sortedPairs(:, 1));
disp(sortedEmitters);
This gives you a list of sorted emitters according to their frequency values.
  1 comentario
slow_hand
slow_hand el 30 de Mzo. de 2024 a las 11:55
Thank you Rahul, very well defined answer!

Iniciar sesión para comentar.

Más respuestas (1)

Parag Jhunjhunwala
Parag Jhunjhunwala el 26 de Jun. de 2023
The following is a sample code to sort the array arr1 and rearrange the elements at corresponding indices of arr2:
arr1=[7 8 5 6];
arr2=[1 2 3 4];
[~,sort_idx] = sort(arr1);
arr2=arr2(sort_idx)
arr2 = 1×4
3 4 1 2
  1 comentario
slow_hand
slow_hand el 30 de Mzo. de 2024 a las 11:55
Thank you Parag, your answer is very well structured as well!

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by