Another Matrix Subtraction Problem
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dan Lynn
am 13 Okt. 2015
Kommentiert: Star Strider
am 13 Okt. 2015
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 Kommentare
Akzeptierte Antwort
Star Strider
am 13 Okt. 2015
Bearbeitet: Star Strider
am 13 Okt. 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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!