Another Matrix Subtraction Problem
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
note: I asked a similar question earlier, but now I have a problem where the numbers in the first column do not start with 1 and followed by counting numbers (2, 3, 4..etc.)
I have this matrix:
67 0
67 2
67 3
78 3
78 4
90 1
90 6
In the second column, I need to find the highest and lowest number that are associated with the same number in the first column, and then subtract the two. In row 1, the numbers 67 and 0 exist. In row 3, the numbers 67 and 3 exists. I need to subtract the 3 and the 0 and get 3 as my output.
The output should be a matrix that looks like this:
67 3
78 1
90 5
Also if a number in the first column only occurs once
67 0
67 2
67 3
78 3
78 4
90 1
90 6
92 5
Then the number in the second column associated with it in the output matrix should be a 0.
67 3
78 1
90 5
92 0
0 commentaires
Réponse acceptée
Star Strider
le 13 Oct 2015
Modifié(e) : Star Strider
le 13 Oct 2015
As long as the first column are integers, this will work:
M = [ 67 0
67 2
67 3
78 3
78 4
90 1
90 6
92 5];
Mu = unique(M(:,1));
R = accumarray(Mu, [1:length(Mu)], [], @(x)[max(M(M(:,1)==Mu(x),2))-min(M(M(:,1)==Mu(x),2))]);
Result = [Mu R(Mu)]
Result =
67 3
78 1
90 5
92 0
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrices and Arrays 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!