Sort values in a cell array
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sergio Rojas Blanco
il 21 Lug 2022
Modificato: Bruno Luong
il 21 Lug 2022
Hi guys, It's amazing how much some people know.
Well, this is my case:
I have a vector. Suppose this:
A=[90,45,38,51];
To this vector correspond another vector. Some values may be duplicates but it is not known how many.
B=[8,1,8,3]
So, I want to sort them and set the initial values in a cell array. The result would be this:
C={[45],[51],[90,38] }
I wish I didn't have to use loop.
Thank you all for your help, especially helping me with this :)
0 Commenti
Risposta accettata
Più risposte (1)
Bruno Luong
il 21 Lug 2022
Modificato: Bruno Luong
il 21 Lug 2022
Another method
B=[8,1,8,3];
A=[90,45,38,51];
[Bs,i]=sort(B);
mat2cell(A(i),1,diff(find([true,diff(Bs)>0,true])))
% or more compact
mat2cell(A(i),1,diff(find([1,diff(Bs),1])))
Vedere anche
Categorie
Scopri di più su Shifting and Sorting Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!