How to find the minimum number in matrix among the repeated array

2 visualizaciones (últimos 30 días)
Alex Rob
Alex Rob el 2 de Mzo. de 2017
Editada: Stephen23 el 2 de Mzo. de 2017
Martix A is as follows:
A = [36 2 896 965
36 2 965 1354
36 4 1354 1400
36 3 1400 1450
36 2 1450 1700
36 2 1700 1800
36 5 1800 1850
88 2 30 45
88 2 45 110
88 4 110 560
11 5 66 73
11 3 73 90
];
I want to form a new matrix B such as:
B = [36 2 896 458
36 4 1354 46
36 3 1400 50
36 2 1450 350
36 5 1800 50
88 2 30 80
88 4 110 450
11 5 66 7
11 3 73 17
];
First and second column on matrix B: It found each unique ID (first column) according to the unique array of the second column Third column: minimum corresponded array from unique third columns of matrix A Fourth column: is the difference between two value of the fourth column from matrix A of two similar arrays of the second row
For example:
% 458 = 1354 - 896
% 46 = 1450 - 13545
% 50 = 1450 - 1400
% 350 = 1800 - 1450
  1 comentario
Alex Rob
Alex Rob el 2 de Mzo. de 2017
I wrote this code:
B = grpstats(A, A(:, 1:2), 'min')
But, somehow it works based on the only unique value of column 2
Any thought or help?

Iniciar sesión para comentar.

Respuestas (1)

Stephen23
Stephen23 el 2 de Mzo. de 2017
Editada: Stephen23 el 2 de Mzo. de 2017
This will give you the first three columns:
A = [...
36,2,896,965
36,2,965,1354
36,4,1354,1400
36,3,1400,1450
36,2,1450,1700
36,2,1700,1800
36,5,1800,1850
88,2,30,45
88,2,45,110
88,4,110,560
11,5,66,73
11,3,73,90
];
[~,idx,idy] = unique(A(:,1:2),'rows','stable');
B = A(idx,:)
B(:,3) = accumarray(idy,A(:,3),[],@min)
But I really have no idea what you want for the fourth column. What does "the difference between two value of the fourth column from matrix A of two similar arrays of the second row" mean ? What are similar rows ? Why these numbers?
458 = 1354 - 896
46 = 1450 - 13545
13545 is not in your array A: how did you get this value?

Categorías

Más información sobre Resizing and Reshaping Matrices 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!

Translated by