Sort a matrix based on unique values
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
Lets assume I have matrix M:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/281819/image.jpeg)
And I want to fill in matrix O:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/281820/image.jpeg)
I want to sort the final matrix based on the first row in matrix M and O. If I just fill in matrix O without sorting, I get this result:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/281821/image.jpeg)
This is wrong, because colum 3 should be NaN while column 4 should have the values from matrix O, because there is a "match" between the first rows (1004 and 1004).
I hope it was clear. Thanks
2 Commenti
Stephen23
il 4 Apr 2020
Use ismember.
If you had given data as text then I would have shown you how, but I can't do anything with data in screenshots.
Risposta accettata
the cyclist
il 4 Apr 2020
I think you can use the ismember function to do what you want. For example,
[tf,loc] = ismember([1001 1002 1004],[1001 1002 1003 1004 1005]);
will result in
loc = [1 2 4]
skipping the 3rd column as you want.
So, you probably want something like
[tf,loc] = ismember(O(1,:),M(1,:));
to find your indices.
3 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrices and Arrays 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!