Comparing 2 columns of a matrix
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Gautami Golani
le 23 Fév 2019
Commenté : Star Strider
le 24 Fév 2019
I want to compare 2 columns of my matrix as shown below:
![mat.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/205518/mat.png)
For example, I have the number 4 in the first column occurring thrice. I need to compare it's corresponding values in column 2 and obtain the minimum value among them. I then need to store that value in another matrix.
Could someone suggest me a way to get this desired result.
Thanks and regards
0 commentaires
Réponse acceptée
Star Strider
le 23 Fév 2019
A = [randi(10, 20, 1) rand(20,1)] % Create Data
[A1u,~,idx] = unique(A(:,1)); % Unique Values & Indices
MinVals = accumarray(idx, A(:,2), [], @(x)min(x)); % Find Minimum Of Each
Result = [A1u, MinVals] % Desired Result
Use your own matrix for ‘A’.
4 commentaires
Plus de réponses (1)
Image Analyst
le 23 Fév 2019
You can do this easily with unique() and histogram() or histcounts(). Since you didn't include your data in a way that's easy for us to code up, I assume you'd prefer to do it yourself, so give it a try. If you run into problems, attach your data and explain the problem(s).
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!