Another Matrix Subtraction Problem
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dan Lynn
el 13 de Oct. de 2015
Comentada: Star Strider
el 13 de Oct. de 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 comentarios
Respuesta aceptada
Star Strider
el 13 de Oct. de 2015
Editada: Star Strider
el 13 de Oct. de 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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!