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

조회 수: 3 (최근 30일)
Alex Rob
Alex Rob 2017년 3월 2일
편집: Stephen23 2017년 3월 2일
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에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by