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

3 次查看(过去 30 天)
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 个评论
Alex Rob
Alex Rob 2017-3-2
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?

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2017-3-2
编辑:Stephen23 2017-3-2
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?

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by